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

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

问题描述

我有一个包含一个复选框列的数据网格。我通过使用多选复选框一次对所有行进行修改。它工作。但是当应用运行时,我无法从该复选框列中获取该值,因为我不知道如何访问数据列。任何人都可以通过一种方式来获得复选框值(true / false)。
这是我迄今为止所做的。




代码:xaml

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

< DataGridTemplateColumn>
< DataGridTemplateColumn.Header>
< CheckBox x:Name =headerCheckBox/>
< /DataGridTemplateColumn.Header>
< DataGridTemplateColumn.CellTemplate>
< DataTemplate>
< CheckBox Name =chkSelectAllHorizo​​ntalAlignment =CenterIsChecked ={Binding IsChecked,ElementName = headerCheckBox,Mode = OneWay}/>
< / DataTemplate>
< /DataGridTemplateColumn.CellTemplate>
< / DataGridTemplateColumn>

< /DataGrid.Columns>

代码:C#

 (int i = 0; i< datagridview.Items.Count; i ++)
{
ÇategoryDataCD =(ÇategoryData)datagridview.Items [i];
if(CD.Check == true)
{
//总是返回false甚至检查
}
}


解决方案

我认为这里有很多奇怪的事情。
我真的不明白为什么你的行复选框被调用chkSelectAll
我建议你尝试得到以下内容:每个复选框都可以被检查,不被自己检查。当您点击标题复选框时,所有列复选框将获得相同的状态(如果至少有一个未被选中,则检查是否已检查所有行复选框)。



如果这是正确的,比你应该做以下:
由于Siderite Zackwehdex提到(我的意思是他的意思),你应该将Checkbox IsChecked值绑定到行的底层ViewModel的属性。



比您的标题复选框还应绑定到Viewmodel的属性,该属性包含如下所示的可观察集合:

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

集合项目的IsSelected-Property-Setter - ViewModel必须通知ParentViewModel(其中包含Collection)IsSelected / IsChecked(但是您想要调用它)State已更改。所以PropertyNameAllSelected的PropertyChanged事件将被调用。



收集项ViewModel属性可能如下所示:

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

两个CheckBox(Header Template和一个在CellTemplate)绑定与 Mode = Twoway


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.

Code: 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>

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

解决方案

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

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.

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

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.

The Collection Items ViewModel Property could look like this:

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

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

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

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