WPF的DataGrid TemplateColumn中使用复选框,并选择在头所有-checkbox使用MVVM光 [英] WPF DataGrid TemplateColumn with CheckBoxes and Select All -checkbox in header using MVVM Light

查看:1964
本文介绍了WPF的DataGrid TemplateColumn中使用复选框,并选择在头所有-checkbox使用MVVM光的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要创建一个复选框列一个DataGrid。在该复选框列的标题,我想有一个检查/取消所有复选框型。

i'm trying to create a DataGrid with a column of checkboxes. On that CheckBox-column's header, i'd like to have a "check/uncheck all"-type of checkbox.

应用程序使用WPF,.NET 4和MVVM光。

App is using WPF, .NET 4 and MVVM Light.

这里的code:

XAML:

<DataGrid x:Name="dgReportList" ItemsSource="{Binding Path=ReportListItems}">
<DataGrid.Columns>
    <DataGridTextColumn Header="*SomeText"  Binding="{Binding Path=SourceReport.Name}" />
    <DataGridTemplateColumn>
        <DataGridTemplateColumn.Header>
            <CheckBox IsChecked="{Binding Source={StaticResource Locator},     
                  Path=MainWindowViewModel.CheckAll, Mode=TwoWay, 
                  UpdateSourceTrigger=PropertyChanged}"  />
        </DataGridTemplateColumn.Header>
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <CheckBox IsChecked="{Binding Path=IsSelected, Mode=TwoWay, 
             UpdateSourceTrigger=PropertyChanged}">                                    
          </CheckBox>
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
   </DataGridTemplateColumn>

...

视图模型:

    private ObservableCollection<ReportListItemModel> _reportListItems;

    public ObservableCollection<ReportListItemModel> ReportListItems
    {
        get
        {
            return this._reportListItems;
        }
        private set
        {
            this._reportListItems = value;
            this.RaisePropertyChanged("ReportListItems");
        }
    }

    ....

    public bool CheckAll
    {
        get { return this._checkall; }
        set 
        {
            this._checkall = value; 

            foreach (var reportListItemModel in ReportListItems)
            {
                reportListItemModel.IsSelected = this._checkall;
            }
        }
    }

型号:

public class ReportListItemModel
{
    public EnvironmentConfigurationModel TargetConfigurationModel { get; set; }
    public ReportModel TargetReport { get; set; }

    public EnvironmentConfigurationModel SourceConfigurationModel { get; set; }
    public ReportModel SourceReport { get; set; }

    private bool _isSelected;

    public bool IsSelected
    {
        get { return _isSelected; }
        set 
        {
            _isSelected = value;
        }
    }
}

我的想法是在DataGrid绑定到类型ReportListItemModel的的ObservableCollection。 ReportListItemModel包含一个公开布尔属性IsSelected,这是我希望得到必然的复选框。

My idea is to bind the DataGrid to a ObservableCollection of type ReportListItemModel. ReportListItemModel contains a public boolean property "IsSelected", which i want to get bound to the checkbox.

方案:
作为一个用户,我希望能够选择(或取消)的所有行通过点击位于标题行的复选框。

Scenario:
As a user, i want to be able to select (or deselect) all the rows by clicking on the checkbox located at the header row.

测试:
任务:单击页眉复选框,当它的状态为未选中
。 预计:对个别行的所有复选框得到检查
。 实际:只有在标题行复选框选中获取

Tests:
Task: Click the "Header-Checkbox", when it's state is unchecked.
Expected: All checkboxes on individual rows get checked.
Actual: Only the checkbox on header row gets checked.

任务:单击页眉复选框,它的状态被选中时
预计:对个别行的所有复选框选中获得
。 实际:只有在标题行复选框被选中。

Task: Click the "Header-Checkbox", when it's state is checked.
Expected: All checkboxes on individual rows get unchecked.
Actual: Only the checkbox on header row gets unchecked.

无论是全选-checkbox并在行checboxes导致内部模型的属性被设置为预期。这只是没有得到绑定到视图。我有一种毛骨悚然的感觉,我有我的模型和视图模型设置搞笑不知何故,即使DataGridTextColumn不会从模型中获得的价值都OK。

Both the "select all"-checkbox and the checboxes on the rows cause the property within model to be set as expected. This just doesn't get bound to the View. I have a creepy feeling that i have my model and viewmodel set funny somehow, even though the DataGridTextColumn does get the value from model a-ok.

我很乐意提供所需的任何额外的code或信息!

I'm happy to provide any additional code or info required!

N.B。我刚开始与WPF,MVVM等,所以如果有一些根本性的缺陷在这里,我很高兴听到这个消息。

N.B. I'm just starting up with WPF, MVVM etc., so if there's some fundamental flaw here, i'd be happy to hear about it.

编辑:
编辑以增强可读性...


Edited for readability...

推荐答案

如果你已经得出 ReportListItemModel 从INotifyPropertyChanged的再提升属性更改事件从 IsSelected 属性setter: RaisePropertyChange(IsSelected)

这篇关于WPF的DataGrid TemplateColumn中使用复选框,并选择在头所有-checkbox使用MVVM光的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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