我在 Silverlight xaml 中有一个 DataGrid,其中包含三个名为 Active(checkbox)、name(Textcolumn)、ID (Textcolumn) 的列和数据网格外部的 Save 按钮。当我尝试循环/迭代数据网格行以使用 GetCellContent 获取复选框的值时,出现以下错误。GetCellContent 对前 20 行工作正常,但之后会出现 NullReferenceException 错误。请帮忙。
System.NullReferenceException: Object reference not set to an instance of an object.
XAML
<StackPanel Orientation="Horizontal">
<Button Content ="Save" Click="SavePopUp_Click"/>
<TextBlock x:Name="GridHeader" Style="{StaticResource GridHeaderStyle}" Margin="0,0,0,0" Width="445" Foreground="White" HorizontalAlignment="Right" TextAlignment="Center" Visibility="Collapsed"/>
</StackPanel>
<sdk:DataGrid x:Name="dgloss">
<sdk:DataGrid.RowDetailsTemplate>
<DataTemplate>
<Border>
<Border Margin="2" Padding="2" BorderBrush="#AAAAAA" BorderThickness="2" CornerRadius="4">
</Border>
</Border>
</DataTemplate>
</sdk:DataGrid.RowDetailsTemplate>
<sdk:DataGrid.Columns>
<!--<sdk:DataGridCheckBoxColumn IsReadOnly="False" Header="Active" Binding="{Binding Active, Mode=TwoWay}" CanUserResize="False" />-->
<sdk:DataGridTemplateColumn Header ="Active">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox x:Name="chkcode" Tag="{Binding Active}" Checked="HandleCheck" Unchecked="HandleUnchecked" IsThreeState="False" HorizontalAlignment ="Center" VerticalAlignment="Center" IsChecked="{Binding Active, Mode=TwoWay}" />
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
<sdk:DataGridTextColumn IsReadOnly="True" SortMemberPath="Name" Header="Name" Binding="{Binding Name, Mode=TwoWay}" Foreground="White"/>
<sdk:DataGridTextColumn x:Name="ID" IsReadOnly="True" SortMemberPath="ID" Header="ID" Binding="{Binding ID, Mode=TwoWay}" Foreground="White" Visibility="Visible"/>
</sdk:DataGrid.Columns>
</sdk:DataGrid>
保存按钮点击事件
private void SavePopUp_Click(object sender, RoutedEventArgs e)
{
IEnumerable list = dgloss.ItemsSource as IEnumerable;
List<string> lstFile = new List<string>();
foreach (var row in list)
{
bool IsChecked = (bool)((CheckBox)dgloss.Columns[0].GetCellContent(row)).IsChecked;
if (IsChecked)
{
string id = ((TextBlock)grdLossCodelist.Columns[2].GetCellContent(row)).Text;
lstFile.Add(id);
}
}
}