简体   繁体   English

WPF绑定DataGridCheckBoxColumn

[英]WPF Binding DataGridCheckBoxColumn

I am having difficulty binding a DataGridCheckBoxColumn in a DataGrid in WPF. 我在WPF的DataGrid中绑定DataGridCheckBoxColumn时遇到困难。

What I am trying to do is have a "Select All" button to check all the items in the grid. 我想做的是有一个“全选”按钮来检查网格中的所有项目。

<Button Grid.Row="1" Grid.Column="0" Content="Select All In List" HorizontalAlignment="Stretch" Command="{Binding SelectAll}"></Button>

In my ViewModel I have a Command that is called from the button. 在我的ViewModel中,我有一个从按钮调用的Command。

 public ICommand SelectAll { get; set; }
 private void OnSelectAll(object obj)
 {
      foreach (var item in EducationLeaflets)
      {
          item.Selected = true;
      }

      OnPropertyChanged("EducationLeaflets");
 }

This is my property from my ViewModel that I bind my DataGrid to: 这是我从ViewModel绑定到DataGrid的属性:

public ObservableCollection<LeafletListModel> EducationLeaflets { get; private set; }

My DataGrid with a DataGridCheckBoxColumn as the first column. 第一列为DataGridCheckBoxColumn的My DataGrid。

<DataGrid Grid.Row="0" Grid.Column="0"
                          AutoGenerateColumns="False"
                          EnableRowVirtualization="True"
                          ItemsSource="{Binding EducationLeaflets}"
                          RowDetailsVisibilityMode="VisibleWhenSelected"
                          HorizontalAlignment="Stretch"
                          VerticalAlignment="Stretch"
                          Grid.ColumnSpan="3" Background="White" HorizontalGridLinesBrush="#FFF0F0F0" VerticalGridLinesBrush="#FFF0F0F0">
                    <DataGrid.Columns>
                        <DataGridCheckBoxColumn 
                            Binding="{Binding Path=Selected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
                        </DataGridCheckBoxColumn>
                        <DataGridTextColumn 
                            Binding="{Binding Id}"
                            Header="RecordId"
                            Width="SizeToHeader" />
                        <DataGridTextColumn 
                            Binding="{Binding Name}"
                            Header="Name"
                            Width="*" />
                    </DataGrid.Columns>
                </DataGrid>

Also the model that is displayed in each grid row. 以及在每个网格行中显示的模型。

public class LeafletListModel: ListModel
{
    public LeafletListModel(int id, string name, DateTime bpsDrugsUpdated):base(id, name)
    {
        BpsDrugsUpdated = bpsDrugsUpdated;
    }

    public bool Selected { get; set; } 

    public DateTime BpsDrugsUpdated { get;private set; }

}

When I click the button the items in the DataGrid are not checked as I would like. 当我单击按钮时,DataGrid中的项目没有按照我的意愿进行检查。 Thank you for your help. 谢谢您的帮助。

It is not EducationLeaflets that changes - it stays the same ObservableCollection as before clicking SelectAll . 不是EducationLeaflets会发生变化-它与单击SelectAll之前保持相同的ObservableCollection。 Even its content does not change (this would be reflected in the CollectionChanged event from the ObservableCollection. 即使它的内容也不会改变(这将反映在ObservableCollection的CollectionChanged事件中。

What actually changes are the individual items in the ObservableCollection. 实际更改的是ObservableCollection中的各个项目。 And since these do not implement INotifyPropertyChanged, the update will not be reflected in the Views. 并且由于这些未实现INotifyPropertyChanged,因此更新将不会反映在视图中。

So, if you make LeafletListModel implement INotifyPropertyChanged , it should work as expected. 因此,如果使LeafletListModel实现INotifyPropertyChanged ,则它将按预期工作。

public class LeafletListModel: ListModel, INotifyPropertyChanged
{
    private bool _selected;

    public LeafletListModel(int id, string name, DateTime bpsDrugsUpdated):base(id, name)
    {
        BpsDrugsUpdated = bpsDrugsUpdated;
    }

    public bool Selected
    {
        get { return _selected; }
        set
        {
            if (_selected != value)
            {
                _selected = value;
                OnPropertyChanged();
            }
        }
    }

    public DateTime BpsDrugsUpdated { get; private set; }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

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

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