使用Silverlight / c#wpf遍历/循环Datagrid行以获得复选框列的值:NullReferenceException错误 [英] Iterate/Loop through Datagrid rows to get the value of checkbox column using Silverlight/ c# wpf: NullReferenceException error

查看:58
本文介绍了使用Silverlight / c#wpf遍历/循环Datagrid行以获得复选框列的值:NullReferenceException错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Silverlight xaml中有一个DataGrid,其中包含三列,分别为Active(复选框),name(Textcolumn),ID(Textcolumn)和datagrid外部的Save按钮。当我尝试循环/遍历datagrid行以使用GetCellContent获取复选框的值时,出现以下错误。 GetCellContent对于前20行正常工作,但此后给出NullReferenceException错误。请帮忙。

I have a DataGrid in Silverlight xaml which contains three columns named Active(checkbox), name(Textcolumn), ID (Textcolumn) and a Save button outside the datagrid. I am getting the following error when I try to loop/iterate through the datagrid rows to get the value of the checkbox using GetCellContent. GetCellContent works fine for first 20 rows but gives the NullReferenceException error after that. Please help.

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);

}
}    
}


推荐答案

我遇到了同样的问题,并确定非文本单元格(复选框,按钮等)仅在呈现后才会返回其值,否则将返回null。此外,实际上仅显示控件上显示的DataGrid行。因此,在您的情况下,我敢打赌,您的DataGrid的大小应使其一次显示20行,因此第21行返回null。

I had the same issue and determined that non-text cells (checkboxes, buttons, etc.) will only return their value if they are rendered, otherwise they return null. Further, only the rows of the DataGrid that are displayed on the control are actually rendered. So in your case, I would bet that your DataGrid is sized such that it displays 20 rows at a time, so the 21st row returns null.

通过ItemSource而不是DataGrid本身进行迭代。最终,无论如何,这都是一个更好的实现,以免通过将数据访问与UI混合而违反MVVM。

What I wound up doing is iterating through the ItemSource instead of the DataGrid itself. Ultimately this is a better implementation anyway so as to not violate MVVM by mixing data access with UI.

但是,如果您确实希望/需要直接使用DataGrid来做到这一点,您可以强制单元格以代码形式呈现:

However, if you really want/need to do it using the DataGrid directly, you can force the cell to render in code:

            foreach (var column in dataGrid.Columns)
            {
                DataGridTemplateColumn dgtColumn = null;
                DataGridBoundColumn dgbColumn = null;

                dgbColumn = column as DataGridBoundColumn;

                if (dgbColumn == null)
                {
                    dgtColumn = column as DataGridTemplateColumn;
                    CheckBox checkbox = (CheckBox)dgtColumn.CellTemplate.LoadContent();
                }
             }

这篇关于使用Silverlight / c#wpf遍历/循环Datagrid行以获得复选框列的值:NullReferenceException错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆