public class VTHelper()
//GetParentObject方法,获取父控件方法。该方法将根据当前控件,遍历查找其父控件是否存在。参数1是表示当前子控件名,参数2是要查询父控件名;使用VisualTreeHelper.GetParent方法获取当前父控件。
public T GetParentObject
(DependencyObject obj, string name) where T : FrameworkElement
DependencyObject parent = VisualTreeHelper.GetParent(obj);
while (parent != null)
if (parent is T && (((T)parent).Name == name | string.IsNullOrEmpty(name)))
return (T)parent;
parent = VisualTreeHelper.GetParent(parent);
return null;
//GetChildObject,获取子控件方法。该方法将根据当前控件,遍历查找其子控件是否存在。参数1是表示当前父控件名,参数2是要查询子控件名;
public T GetChildObject(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;
grandChild = GetChildObject(child, name);
if (grandChild != null)
return grandChild;
return null;
//GetChildObjects方法,该方法将把所有子控件作为List集合返回到客户端。其中第一个参数是父控件参数,而第二个参数是特定子控件名称,如果需要遍历全部子控件,第二个参数留空即可。
public List GetChildObjects(DependencyObject obj, string name) 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).Name == name || string.IsNullOrEmpty(name)))
childList.Add((T)child);
childList.AddRange(GetChildObjects(child,""));
return childList;
private void btModifyChilds_Click(object sender, RoutedEventArgs e)
Globals VTHelper = new Globals();
List textblock = VTHelper.GetChildObjects(this.LayoutRoot, "");
foreach (TextBlock tmpTextblock in textblock)
tmpTextblock.FontSize += 6;
Type t =
typeof
(MyChild);
bool
是否是某个的子类 = t.IsSubclassOf(
typeof
(MyBase));
bool
是否是枚举 = t.GetProperty(
"TestState"
).PropertyType.IsEnum;
风中代表自由、寻觅代表不断前进~!