VisualTreeHelper提供可用于在 Silverlight 可视化树中遍历对象关系(以及子对象或父对象轴)的实用工具方法。
使用VisualTreeHelper的 GetChildrenCount 和GetChild 方法
public static int GetChildrenCount( DependencyObject reference )
public static DependencyObject GetChild( DependencyObject reference, int childIndex )
例子:清空页面所有textbox的值
private void Clear(DependencyObject control)
{
TextBox txt = control as TextBox;
if (txt != null)
{
txt.Text = "";
}
int txtCount = VisualTreeHelper.GetChildrenCount(control);
for (int i = 0; i < txtCount; i++)
{
DependencyObject txtSingle = VisualTreeHelper.GetChild(control, i);
Clear(txtSingle );
}
}