前两种方式对我有用,下面提到的第三和第四点都不起作用
这是我的代码
<DataGridTemplateColumn>
<DataGridTemplateColumn.Header>
<CheckBox Content="Included" x:Name="headerCheckBox" />
</DataGridTemplateColumn.Header>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox Name="chkselectAll" IsChecked="{Binding IsChecked,ElementName=headerCheckBox,Mode=OneWay}"></CheckBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
我可以在这里得到一些帮助吗?我如何在xaml中实现它?
我希望像这样选择所有复选框行为
https://www.codeproject.com/Articles/42437/Toggling-the-States-of-all-CheckBoxes-Inside-a-Dat#
答案 0 :(得分:2)
您应该将CheckBox
中的CellTemplate
绑定到数据对象的source属性:
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox Name="chkselectAll" IsChecked="{Binding IsChecked, UpdateSourceTrigger=PropertyChanged}"></CheckBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
确保数据对象实现INotifyPropertyChanged
接口:
public class Item : INotifyPropertyChanged
{
private bool _isChecked;
public bool IsChecked
{
get { return _isChecked; }
set { _isChecked = value; OnPropertyChanged(); }
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
然后,您可以将标头CheckBox
的属性绑定到视图模型的属性,只要DataGrid
的源集合中的项发生更改,就会设置该属性,例如:
<DataGridTemplateColumn.Header>
<CheckBox Content="Included"
x:Name="headerCheckBox"
IsChecked="{Binding DataContext.AllChecked, RelativeSource={RelativeSource AncestorType=DataGrid}}"/>
</DataGridTemplateColumn.Header>
public class ViewModel : INotifyPropertyChanged
{
public ViewModel()
{
Items = new ObservableCollection<Item>();
Items.CollectionChanged += Items_CollectionChanged;
//add the items..:
Items.Add(new Item());
Items.Add(new Item() { IsChecked = true });
Items.Add(new Item());
}
private void Items_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.NewItems != null)
{
foreach (object item in e.NewItems)
{
(item as INotifyPropertyChanged).PropertyChanged += new PropertyChangedEventHandler(item_PropertyChanged);
}
}
if (e.OldItems != null)
{
foreach (object country in e.OldItems)
{
(country as INotifyPropertyChanged).PropertyChanged -= new PropertyChangedEventHandler(item_PropertyChanged);
}
}
}
private void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
OnPropertyChanged("AllChecked");
}
public ObservableCollection<Item> Items { get; private set; }
public bool AllChecked
{
get { return Items.All(x => x.IsChecked); }
set
{
foreach (var item in Items)
item.IsChecked = value;
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}