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

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

问题描述

我有一个数据网格,其中包括一个复选框列。我通过使用多选复选框一次检查所有行进行了修改,以这种形式。和它的工作。但我无法摆脱,当应用程序正在运行,因为我不知道如何访问数据列复选框列的值。谁能帮我一种方式来获得的复选框值(真/假)。
这是我做的那么远。




码:XAML

 < D​​ataGrid.Columns> 
< D​​ataGridTextColumn绑定={绑定路径= ID}标题=ID能见度=隐藏/>
< D​​ataGridTextColumn绑定={绑定路径=类别}标题=类别WIDTH =320/>
<! - < D​​ataGridCheckBoxColumn绑定={绑定路径=检查}WIDTH =*/> - >

< D​​ataGridTemplateColumn>
< D​​ataGridTemplateColumn.Header>
<复选框X:NAME =headerCheckBox/>
< /DataGridTemplateColumn.Header>
< D​​ataGridTemplateColumn.CellTemplate>
<&DataTemplate的GT;
<复选框名称=chkSelectAll的Horizo​​ntalAlignment =中心=器isChecked{结合器isChecked,的ElementName = headerCheckBox,模式=单向}/>
< / DataTemplate中>
< /DataGridTemplateColumn.CellTemplate>
< / DataGridTemplateColumn>

< /DataGrid.Columns>



代码:C#



 的for(int i = 0; I< datagridview.Items.Count;我++)
{
ÇategoryDataCD =(ÇategoryData)datagridview.Items [I]
如果(CD.Check ==真)
{
//它始终返回false,即使检查
}
}


解决方案

下面很多事情在我看来,那种怪异。
我真的不知道为什么你行复选框被称为chkSelectAll
我建议你尝试得到以下几点:每个复选框可以检查,通过自理选中。 (如果所有的行都复选框被选中选中如果至少有一个是选中,并且未选中)。当你打的头复选框,然后所有列复选框得到同样的国家。



如果这是正确的,比你应该做到以下几点:$ b​​ $ b菱铁矿Zackwehdex提到(我东西,他的意思是),你应该复选框器isChecked值绑定到该行的基本视图模型的属性。



比你的头复选框也应该是绑定到视图模型的财产持有观察的集合如下所示:

 公共BOOL AllSelected 
{
{返回this.MyCollection.Any(项目=>!item.IsSelected)!; }

{
无功切换= this.MyCollection.Any(项目=>!item.IsSelected);
的foreach(在this.MyCollection.Where(项目=> VAR ITM;!item.IsSelected =切换))
itm.IsSelected =切换;
}
}



在收藏物品─的IsSelected-财产二传手视图模型必须通报ParentViewModel(持有的集合),一个IsSelected /器isChecked(但是你要调用它)状态已更改。所以,一个PropertyChanged事件的属性名AllSelected将上调至



收集项目视图模型属性看起来是这样的:

 公共BOOL IsSelected 
{
{返回_isSelected; }

{
_isSelected =价值;
RaisePropertyChanged(IsSelected);
ParentViewModel.RaisePropertyChanged(IsSelected);
}
}



两个复选框(一个在页眉模板和一个在CellTemplate)是绑定模式=幅增频双向


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天全站免登陆