这个问题纠结很长时间,今个解决了啊!归结到底还是对WPF不是太了解啊,当你做完以后才发现原来是自己想的复杂了,所以啊考虑问题还是要从这门语言的本身出发,
这样有时候就少走些弯路了。^^
xaml对应代码:
<DataGrid>
<DataGrid.Columns>
<DataGridCheckBoxColumn Binding="{Binding Path=IsSelected,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGridRow}}}">
<DataGridCheckBoxColumn.HeaderTemplate>
<DataTemplate>
<CheckBox x:Name="ckbSelectedAll" Content="全选" Checked="ckbSelectedAll_Checked" Unchecked="ckbSelectedAll_Unchecked" IsChecked="False" />
</DataTemplate>
</DataGridCheckBoxColumn.HeaderTemplate>
</DataGridCheckBoxColumn>
<DataGridTextColumn Header="序号" Width="*" Binding="{Binding SequenceNumber}" IsReadOnly="True"/> <DataGridTextColumn Header="编号" Width="*" Binding="{Binding Number}" IsReadOnly="True"/>
.....................................
...................................
<DataGrid.Columns>
<DataGrid>
C#代码:
/// <summary> /// 全选 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ckbSelectedAll_Checked(object sender, RoutedEventArgs e) { this.dgrid.SelectAll(); } /// <summary> /// 全不选 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ckbSelectedAll_Unchecked(object sender, RoutedEventArgs e) { this.dgrid.UnselectAll(); }
你可以试一下了。
下面是我的软件截图: