【问题标题】:WPF Binding DataGridCheckBoxColumnWPF 绑定 DataGridCheckBoxColumn
【发布时间】:2018-06-20 11:15:48
【问题描述】:

我在 WPF 的 DataGrid 中绑定 DataGridCheckBoxColumn 时遇到了困难。

我想要做的是有一个“全选”按钮来检查网格中的所有项目。

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

在我的 ViewModel 中,我有一个从按钮调用的命令。

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

      OnPropertyChanged("EducationLeaflets");
 }

这是我将 DataGrid 绑定到的 ViewModel 中的属性:

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

我的 DataGrid 以 DataGridCheckBoxColumn 作为第一列。

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

还有每个网格行中显示的模型。

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

}

当我单击按钮时,DataGrid 中的项目不会按照我的意愿进行检查。感谢您的帮助。

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    改变的不是EducationLeaflets - 它保持与单击SelectAll 之前相同的ObservableCollection。甚至它的内容也不会改变(这将反映在 ObservableCollection 的 CollectionChanged 事件中。

    实际发生变化的是 ObservableCollection 中的各个项目。并且由于这些没有实现 INotifyPropertyChanged,因此更新不会反映在 Views 中。

    所以,如果你让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));
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-05-08
      • 2012-05-25
      • 2011-02-02
      • 1970-01-01
      • 2013-06-15
      • 2010-11-23
      • 2011-08-30
      • 1970-01-01
      • 2021-07-08
      相关资源
      最近更新 更多