我有一个 wpf 形式的 DataGrid。它有带有复选框的行。我想选择/取消选择选中/取消选中标题复选框的所有行。
但我收到此错误:chk.IsChecked = false 上的“对象引用未设置为对象的实例”。
c#代码:
private void myDataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
CheckBox chk = (CheckBox)this.myDataGrid.Columns[0].GetCellContent(e.Row);
chk.IsChecked = false;
checkboxes.Add(chk);
}
xml代码是:
<Window x:Class="WpfApplication1.Grid"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Grid" Height="300" Width="300">
<Grid>
<DataGrid x:Name="myDataGrid"
VerticalAlignment="Top"
Grid.Column="0"
AutoGenerateColumns="False"
LoadingRow="myDataGrid_LoadingRow"
Loaded="myDataGrid_Loaded">
<DataGrid.Columns>
<DataGridTemplateColumn Width="80">
<DataGridTemplateColumn.Header>
<CheckBox HorizontalAlignment="Center"
Click="chk_Click"
VerticalAlignment="Center"
Name="chckAll">
</CheckBox>
</DataGridTemplateColumn.Header>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox x:Name="chk"
HorizontalAlignment="Center"
HorizontalContentAlignment="Center"></CheckBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="First Name"
Width="100"
Binding="{Binding FirstName}"></DataGridTextColumn>
<DataGridTextColumn Header="Last Name"
Width="100"
Binding="{Binding LastName}"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
提前致谢。