我目前还有另一个与DataGrid相关的问题,但我想我会打开另一个问题
我正在尝试根据另一列的值自动更改复选框的状态,但如何使用DataGridCheckBoxColumn
执行此操作?我使用'IsSelected'但最终意识到这个属性应用于单元格而不是复选框。
这是我的代码
<DataGridCheckBoxColumn Header="Active" Binding="{Binding IsActive}">
<DataGridCheckBoxColumn.CellStyle>
<Style TargetType="DataGridCell">
<Style.Triggers>
<DataTrigger Binding="{Binding SelectedLocation.LocationText}" Value="" >
<Setter Property="IsEnabled" Value="False" />
</DataTrigger>
<DataTrigger Binding="{Binding SelectedLocation.LocationText}" Value="01" >
<Setter Property="IsEnabled" Value="True" />
</DataTrigger>
<DataTrigger Binding="{Binding SelectedLocation.LocationText}" Value="02" >
<Setter Property="IsSelected" Value="True" />
<Setter Property="IsEnabled" Value="False" />
</DataTrigger>
<DataTrigger Binding="{Binding SelectedLocation.LocationText}" Value="03" >
<Setter Property="IsSelected" Value="False" />
<Setter Property="IsEnabled" Value="False" />
</DataTrigger>
</Style.Triggers>
</Style>
</DataGridCheckBoxColumn.CellStyle>
</DataGridCheckBoxColumn>
如您所见,我想在LocationText为2时自动选中复选框,并在LocationText为3时取消选中。
有什么想法吗?
更新1:
当我尝试以下操作时,我想我已经弄明白了:
<DataGridCheckBoxColumn.ElementStyle>
<Style TargetType="CheckBox">
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="VerticalAlignment" Value="Center" />
<Style.Triggers>
<DataTrigger Binding="{Binding SelectedLocation.LocationText}" Value="01" >
<Setter Property="IsChecked" Value="False" />
<Setter Property="IsEnabled" Value="True" />
</DataTrigger>
<DataTrigger Binding="{Binding SelectedLocation.LocationText}" Value="02" >
<Setter Property="IsChecked" Value="True" />
<Setter Property="IsEnabled" Value="False" />
</DataTrigger>
<DataTrigger Binding="{Binding SelectedLocation.LocationText}" Value="03" >
<Setter Property="IsChecked" Value="False" />
<Setter Property="IsEnabled" Value="False" />
</DataTrigger>
</Style.Triggers>
</Style>
</DataGridCheckBoxColumn.ElementStyle>
但无济于事。它会禁用该复选框,但拒绝检查它。
...令人沮丧的
答案 0 :(得分:0)
应该是
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate >
<DataTemplate x:Name="dtAllChkBx">
<CheckBox Name="cbxAll">
<CheckBox.Style>
<Style TargetType="CheckBox">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=wndMain, Path=LocationText , Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Value="" >
<Setter Property="IsEnabled" Value="False" />
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=wndMain, Path=LocationText, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Value="01" >
<Setter Property="IsEnabled" Value="True" />
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=wndMain, Path=LocationText, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Value="02" >
<Setter Property="IsChecked" Value="True" />
<Setter Property="IsEnabled" Value="False" />
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=wndMain, Path=LocationText, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Value="03" >
<Setter Property="IsChecked" Value="False" />
<Setter Property="IsEnabled" Value="False" />
</DataTrigger>
</Style.Triggers>
</Style>
</CheckBox.Style>
</CheckBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
和datacontext必须实现INotifyPropertyChanged 当绑定属性更改时,需要通知属性已更改。