如何从 WPF DataGrid 中的模板列获取值. [英] How to get the value from a Template Column in a WPF DataGrid.

查看:23
本文介绍了如何从 WPF DataGrid 中的模板列获取值.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含复选框列的数据网格.我通过使用多选复选框一次检查所有行对该表单进行了修改.它奏效了.但是当应用程序运行时我无法从该复选框列中获取值,因为我不确定如何访问数据列.任何人都可以帮助我获取复选框值(真/假).这是我目前所做的.

I got a data grid which includes a check box column. I made a modification to that form by using a multiselect checkbox to check all rows at once. And it worked. but i was unable to get the value from that checkbox column when the app is running because i was not sure how to access the data column. can anyone help me with a way to get the check box value (true/false). This is what i did so far.

代码:xaml

<DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Path= Id}" Header="Id" Visibility="Hidden"/>
            <DataGridTextColumn Binding="{Binding Path= Category}" Header="Category" Width="320"/>
            <!--<DataGridCheckBoxColumn Binding="{Binding Path= Check}" Width="*"/>-->

            <DataGridTemplateColumn>
                <DataGridTemplateColumn.Header>
                    <CheckBox x:Name="headerCheckBox" />
                </DataGridTemplateColumn.Header>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <CheckBox Name="chkSelectAll" HorizontalAlignment="Center" IsChecked="{Binding IsChecked, ElementName=headerCheckBox, Mode=OneWay}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

        </DataGrid.Columns>

代码:C#

for (int i = 0; i < datagridview.Items.Count; i++)
            {
                ÇategoryData CD = (ÇategoryData)datagridview.Items[i];
                if (CD.Check == true)
                {
                    //it always returns false even checked 
                }
            }

推荐答案

在我看来,这里有很多奇怪的事情.我真的不明白为什么你的行的复选框被称为chkSelectAll"我建议您尝试获得以下内容:每个复选框都可以自己选中,未选中.当您点击标题复选框时,所有列复选框都会获得相同的状态(如果至少有一个未选中,则为选中,如果所有行复选框都被选中,则为未选中).

Here are many things kind of weird in my opinion. I don't really get why your rows' Checkbox is called "chkSelectAll" I suggest you try to get the following: Each Checkbox can be Checked,UnChecked by themself. When you hit the Headers Checkbox, then all Columns Checkbox get the same State (Checked if at least one was unchecked, and Unchecked if ALL Rows Checkboxes were checked).

如果这是对的,那么您应该执行以下操作:正如Siderite Zackwehdex 提到的(我的意思是他的意思),您应该将Checkbox IsChecked 值绑定到行的底层ViewModel 的属性.

If that is right, than you should do the following: As Siderite Zackwehdex mentioned (I thing that he meant that), you should bind the Checkbox IsChecked Value to a Property of the underlying ViewModel of the Row.

比你的 Headers Checkbox 还应该绑定到 Viewmodel 的一个属性,该属性包含如下所示的 Observable 集合:

Than your Headers Checkbox should be also bind to a Property of the Viewmodel which holds the Observable Collection like the following:

public bool AllSelected
{
    get { return !this.MyCollection.Any(item => !item.IsSelected); }
    set
    {
        var toggle = this.MyCollection.Any(item => !item.IsSelected);
        foreach (var itm in this.MyCollection.Where(item => item.IsSelected != toggle))
            itm.IsSelected = toggle;
    }
}

集合 Items-ViewModel 的 IsSelected-Property-Setter 必须通知 ParentViewModel(持有集合)一个 IsSelected/IsChecked(不管你想怎么称呼它)状态已经改变.这样就会引发 PropertyName "AllSelected" 的 PropertyChanged 事件.

The IsSelected-Property-Setter of the Collections Items-ViewModel MUST notify the ParentViewModel (which holds the Collection) that one IsSelected/IsChecked (however you want to call it) State has changed. So that a PropertyChanged Event for PropertyName "AllSelected" will be raised.

Collection Items ViewModel 属性可能如下所示:

The Collection Items ViewModel Property could look like this:

public bool IsSelected
{
    get { return _isSelected; }
    set
    {
        _isSelected = value;
        RaisePropertyChanged("IsSelected");
        ParentViewModel.RaisePropertyChanged("IsSelected");
    }
}

两个复选框(Header Template 中的一个和 CellTemplate 中的一个)都绑定了 Mode=Twoway

Both CheckBoxes (The one in the Header Template and the one in the CellTemplate) are bind with Mode=Twoway

这篇关于如何从 WPF DataGrid 中的模板列获取值.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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