【问题标题】:Get Checkbox value in Datagrid bound to an ItemsSource in wpf获取绑定到 wpf 中 ItemsSource 的 Datagrid 中的复选框值
【发布时间】:2015-03-12 10:26:59
【问题描述】:

在 WPF 中,我创建了一个绑定到 ItemsSource 的 Datagrid,然后我添加了另一个未绑定到 itemssource 的列。我正在尝试遍历 Datagrid 中的行并获取复选框值(true 或 false)。这是我的 Datagrid 中的一个 sn-p:

<DataGridTemplateColumn x:Name="CheckBoxColumn" Width="Auto">
    <DataGridTemplateColumn.Header>
        <CheckBox Content="Select All" Style="{StaticResource StdCheckBoxStyle}" x:Name="headerCheckBox"/>
    </DataGridTemplateColumn.Header>
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <CheckBox x:Name="checkBoxRow" Margin="45 2 0 0" Style="{StaticResource StdCheckBoxStyle}"
                            IsChecked="{Binding IsChecked, ElementName=headerCheckBox, 
                                        Mode=OneWay}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Binding="{Binding Index, Mode=OneWay}" IsReadOnly="True" Width="Auto" Header="Index"/>

Datagrid 绑定到 ItemsSource,它是 MyObject 的 ICollection。这是我在后面的代码中所做的:

    public IEnumerable<DataGridRow> GetDataGridRows(DataGrid grid)
    {
        var itemsSource = grid.ItemsSource as IEnumerable;
        if (null == itemsSource) yield return null;
        foreach (var item in itemsSource)
        {
            var row = grid.ItemContainerGenerator.ContainerFromItem(item) as DataGridRow;
            if (null != row) yield return row;
        }
    }

    private void OnUpdate(object obj)
    {
      var rows= GetDataGridRows(MyDatagrid); 

        foreach (DataGridRow r in rows)  
        {  
            DataRowView rv = (DataRowView)r.Item;
            foreach (DataGridColumn column in MyDatagrid.Columns)
            {
                if (column.GetCellContent(r) is CheckBox)
                {
                    CheckBox cellContent = column.GetCellContent(r) as CheckBox;
                 //do something
                }
            }
        } 

    }

但是运行这段代码后,我得到一个异常:

Unable to cast object of type '...MyObject' to type 'System.Data.DatarowView'.

我看了一点,在 MSDN 中发现:

the DataItem property of the GridViewRow "Gets the underlying data object to which the GridViewRow object is bound."

我知道问题所在,我知道原因,但找不到解决方法。感谢您的关注。

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    在您的代码中,异常:

    Unable to cast object of type '...MyObject' to type 'System.Data.DatarowView'.
    

    因为这条线而被提出:

    DataRowView rv = (DataRowView)r.Item;
    

    这意味着 r.item 对象是 MyObject 类型。演员表无效。改为使用:

    MyObject rv= r.Item;
    

    [编辑] 答案的第一部分只解决了异常的问题。您必须将数据网格转换为数据网格视图。 要访问单元格的内容:

        foreach (DataGridViewRow r in MyDataGridView.Rows)  
        {  
            foreach (DataGridViewCell c in r.Cells)
            {
                if (c.Value is CheckBox)
                {
                    CheckBox cellContent = c.Value as Checkbox;
                 //do something
                }
            }
        } 
    

    【讨论】:

    • 谢谢,但这并没有解决我的问题。我需要将复选框视为数据网格的一部分,而不是绑定对象列表的一部分。默认情况下,遍历数据网格会产生基于 ItemsSource 的对象。需要改变它。
    • 我明白了,在这种情况下,我建议您使用 DataGridView 而不是 DataGrid 控件,然后使用 Checkbox x = (CheckBox)MyDatagridView.Rows[0].Cells[0].Value;跨度>
    • 好的,我又看了你的帖子。我认为a误读了一些东西。当您运行代码时,它现在会引发任何异常吗?在您提供的示例中,您从不使用 rv 变量。单元格内容是否为空?
    猜你喜欢
    • 2023-03-07
    • 2011-07-21
    • 2011-12-17
    • 1970-01-01
    • 2020-01-30
    • 1970-01-01
    • 1970-01-01
    • 2011-07-10
    • 2013-01-14
    相关资源
    最近更新 更多