WPF查找子控件和父控件方法

一、查找某种类型的子控件,并返回一个List集合
public List GetChildObjects(DependencyObject obj, Type typename) where T : FrameworkElement
{
DependencyObject child = null;
List childList = new List();

for (int i = 0; i <= VisualTreeHelper.GetChildrenCount(obj) - 1; i++)
{
child = VisualTreeHelper.GetChild(obj, i);

if (child is T && (((T)child).GetType() == typename))
{
childList.Add((T)child);
}
childList.AddRange(GetChildObjects(child,typename));
}
return childList;
}
调用:
List

转载于:https://www.cnblogs.com/sjqq/p/7835149.html

你可能感兴趣的:(WPF查找子控件和父控件方法)