47 lines
1.4 KiB
C#
47 lines
1.4 KiB
C#
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Linq;
|
|||
|
|
using System.Text;
|
|||
|
|
using System.Threading.Tasks;
|
|||
|
|
using System.Windows.Media;
|
|||
|
|
using System.Windows;
|
|||
|
|
|
|||
|
|
namespace YY.Admin.Core.Extension
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// 窗体的扩展类
|
|||
|
|
/// </summary>
|
|||
|
|
public static class WindowExtensions
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// 查找子控件
|
|||
|
|
/// </summary>
|
|||
|
|
/// <typeparam name="T">子控件的类型</typeparam>
|
|||
|
|
/// <param name="obj">要找的是obj的子控件</param>
|
|||
|
|
/// <param name="name">想找的子控件的Name属性</param>
|
|||
|
|
/// <returns>目标子控件</returns>
|
|||
|
|
public static T GetChildObject<T>(this DependencyObject obj, string name) where T : FrameworkElement
|
|||
|
|
{
|
|||
|
|
DependencyObject child = null;
|
|||
|
|
T grandChild = null;
|
|||
|
|
|
|||
|
|
for (int i = 0; i <= VisualTreeHelper.GetChildrenCount(obj) - 1; i++)
|
|||
|
|
{
|
|||
|
|
child = VisualTreeHelper.GetChild(obj, i);
|
|||
|
|
|
|||
|
|
if (child is T && (((T)child).Name == name | string.IsNullOrEmpty(name)))
|
|||
|
|
{
|
|||
|
|
return (T)child;
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
grandChild = GetChildObject<T>(child, name);
|
|||
|
|
if (grandChild != null)
|
|||
|
|
return grandChild;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|