WPF绑定DataGridCheckBoxColumn

时间:2018-01-11 07:22:56

标签: c# wpf

我无法在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");
 }

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

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中的项目不会按照我的意愿进行检查。谢谢你的帮助。

1 个答案:

答案 0 :(得分:2)

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

ObservableCollection中的各个项目实际发生了哪些变化。由于这些没有实现INotifyPropertyChanged,更新将不会反映在视图中。

因此,如果您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));
    }
}