DG是控件名称
public T GetVisualChild<T>(DependencyObject parent, Func<T, bool> predicate) where T : Visual
{
int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < numVisuals; i++)
{
DependencyObject v = (DependencyObject)VisualTreeHelper.GetChild(parent, i);
T child = v as T;
if (child == null)
{
child = GetVisualChild<T>(v, predicate);
if (child != null)
{
return child;
}
}
else
{
if (predicate(child))
{
return child;
}
}
}
return null;
}
CheckBox chb = GetVisualChild<CheckBox>(DG, v => v.Name == "cbbSelALL");
if (null != chb)
{
chb.IsChecked = false;
}