![Question](https://i1.social.s-msft.com/globalresources/Images/trans.gif?cver=0%0D%0A)
<DataGrid Name="DG">
<DataGrid.Columns>
<DataGridTemplateColumn Width="70">
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<CheckBox Name="cbbSelALL" Content="全选" Click="cbbSelALL_Click"/>
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding SelFlag}" Name="cbsel"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid>
代码如上; 当我选中列头的CheckBox后,我重新查询绑定数据时需要将列头的CheckBox的选中状态取消,有什么方法吗?
可以从VisualTree一层层地查找到CheckBox,代码如下:
CheckBox chb = GetVisualChild<CheckBox>(DG, v => v.Name == "cbbSelALL"); if (null != chb) { chb.IsChecked = false; }
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; }