未选中WPF复选框上所有未更新的复选框行
本文关键字:复选框 更新 WPF | 更新日期: 2023-09-27 18:07:17
我在WPF c#的数据网格中有一个复选框。
<DataGridCheckBoxColumn Binding="{Binding IsSelected,UpdateSourceTrigger=PropertyChanged}" CanUserSort="False">
<DataGridCheckBoxColumn.ElementStyle>
<Style TargetType="CheckBox" BasedOn="{StaticResource {x:Type CheckBox}}">
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
</Style>
</DataGridCheckBoxColumn.ElementStyle>
<DataGridCheckBoxColumn.HeaderTemplate>
<DataTemplate x:Name="dtAllChkBx">
<CheckBox Name="cbxAll" HorizontalAlignment="Center" Margin="0,0,5,0" IsEnabled="{Binding Path=DataContext.IsCbxAllEnabled,RelativeSource={RelativeSource AncestorType=DataGrid}}"
IsChecked="{Binding Path=DataContext.AllSelected,RelativeSource={RelativeSource AncestorType=DataGrid},UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
</DataGridCheckBoxColumn.HeaderTemplate>
</DataGridCheckBoxColumn>
当我选中All复选框时,当然,它会标记所有的复选框,但是一旦我取消选中一个复选框,All复选框仍然被选中。这应该被制止。我该如何使用WPF c#来实现呢?
如果我理解正确的话-在集合项内更改IsSelected属性后,您应该更新AllSelected值。
所以,你需要一些回调在你所有的项目(事件或动作或任何你想要的机制)和改变get逻辑AllSelected
下面是项目IsSelected属性和构造函数的一些草稿:public bool IsSelected {
get { return isSelected; }
set {
isSelected = value;
OnPropertyChanged();
if (globalUpdate != null) globalUpdate();
}
}
public ItemClass(Action globalUpdate, ...your parameters) {
this.globalUpdate = globalUpdate;
...do smth with your parameters
}
用法示例:
new ItemClass(() => OnPropertyChanged("AllSelected"))
当然,不要忘记AllSelectedgetter
public bool AllSelected {
get { return YourGridItemsCollection.All(item => item.IsSelected); }
现在,当您手动检查所有项目,然后AllSelected将自动选中,并未选中当您取消选中任何项目