简体   繁体   English

在WPF中绑定到ItemsSource的Datagrid中获取Checkbox值

[英]Get Checkbox value in Datagrid bound to an ItemsSource in wpf

In WPF, I created a Datagrid that is bound to an ItemsSource and then I added another column which is not bound to the itemssource. 在WPF中,我创建了一个与ItemsSource绑定的Datagrid,然后添加了另一个未与ItemsSource绑定的列。 I'm trying to iterate over rows in Datagrid and get the checkbox value(true or false). 我试图遍历Datagrid中的行并获取复选框值(true或false)。 Here is a snippet from my Datagrid: 这是我的Datagrid的一个片段:

<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 is bound to an ItemsSource, which is an ICollection of MyObject. Datagrid绑定到ItemsSource,这是MyObject的ICollection。 Here is what I did in code behind: 这是我在后面的代码中所做的:

    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
                }
            }
        } 

    }

But after running this piece of code, I get an exception: 但是在运行这段代码后,我得到一个异常:

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

I looked a little bit and found out that in MSDN: 我看了一下,发现在MSDN中:

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

I know the problem, I know the reason but couldn't find a way to fix it. 我知道这个问题,我知道原因,但是找不到解决方法。 Thanks for your attention. 感谢您的关注。

In your code, the exception: 在您的代码中,例外:

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

is raised because of this line: 由于此行而引发:

DataRowView rv = (DataRowView)r.Item;

Which means the r.item object is of type MyObject. 这意味着r.item对象的类型为MyObject。 The cast is invalid. 强制转换无效。 Use instead: 改用:

MyObject rv= r.Item;

[EDIT] The first part of the answer only solved the problem with the exception. [编辑]答案的第一部分仅解决了异常问题。 You'll have to convert your datagrid to a datagridview. 您必须将datagrid转换为datagridview。 To access the content of the cell: 要访问单元格的内容:

    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
            }
        }
    } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM