我有以下(简化)样式:
<Style x:Key="MyStyle" TargetType="{x:Type CheckBox}">
<Setter Property="Background" Value="Blue" />
</Style>
如果我将它用作我的 DataGridCheckBoxColumn 中的 ElementStyle 和 EditingElementStyle:
<DataGridCheckBoxColumn Binding="{Binding IsEnabled}"
ElementStyle="{StaticResource MyStyle}"
EditingElementStyle="{StaticResource MyStyle}" />
然后,当我选中/取消选中某行的复选框时,我的绑定(bind) IsEnabled
不会切换。如果我删除 ElementStyle、EditingElementStyle 或两者,则绑定(bind)更新没有问题。这是为什么?!
此外,我尝试使用以下代码解决该问题:
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsEnabled}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
但是,问题依然存在。
最佳答案
抱歉死机了,但我想我在 Stack Overflow 上找到了一个更好的解决方案,它可能会帮助那些最终在这个页面上搜索解决方案的人。
https://stackoverflow.com/a/7270548/3082531
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Path=IsSelected, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
我试过了,它对我来说非常有效,比公认的解决方案更简单,而且无需额外点击复选框。
关于c# - WPF DataGrid 中的自定义复选框不更新绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2926533/