Pak*_*man 8 c# wpf binding datagrid
我有以下(简化)风格:
<Style x:Key="MyStyle" TargetType="{x:Type CheckBox}">
<Setter Property="Background" Value="Blue" />
</Style>
Run Code Online (Sandbox Code Playgroud)
如果我在DataGridCheckBoxColumn中将它用作ElementStyle AND EditingElementStyle:
<DataGridCheckBoxColumn Binding="{Binding IsEnabled}"
ElementStyle="{StaticResource MyStyle}"
EditingElementStyle="{StaticResource MyStyle}" />
Run Code Online (Sandbox Code Playgroud)
然后,IsEnabled当我选中/取消选中行的复选框时,我的绑定不会切换.如果我删除ElementStyle,EditingElementStyle或两者,那么绑定更新没有问题.为什么是这样?!
此外,我尝试使用以下代码解决此问题:
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsEnabled}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
Run Code Online (Sandbox Code Playgroud)
但问题仍然存在.
sup*_*_ht 14
对不起,我觉得我在Stack Overflow上找到了一个更好的解决方案,可以帮助人们在这个页面上搜索解决方案.
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Path=IsSelected, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
Run Code Online (Sandbox Code Playgroud)
我尝试了这个,它对我来说非常合适,比公认的解决方案更简单,也不需要额外点击复选框.
Avi*_* P. 11
首先你的断言,如果你删除任何一个ElementStyle或EditingElementStyle解决问题是不正确的,你解决的问题是什么ElementStyle.
问题是,要进行编辑,数据网格必须切换到编辑模板,通常在鼠标单击时进行,但是,由于CheckBox处理鼠标单击事件,数据网格永远不会得到它,并且永远不会进入编辑模式,防止您的更改到达您的数据对象(它保留在数据视图中但不会传递给源数据).
现在您可能会问,默认行为怎么样?好吧,如果你查看ElementStyle属性的默认值,你会注意到它设置了两个IsHitTestVisible并且Focusable为false.这可以防止CheckBox处理改变其状态的鼠标单击(或键盘事件),并允许数据网格接收它们,从而对其进行更改以进入编辑模式并切换到EditingElementStyle不影响可聚焦性和命中可测试性的模式.
查看此博客条目以获取有关如何正确执行此操作的示例.当WPF DataGrid只读CheckBox不是只读时?
| 归档时间: |
|
| 查看次数: |
7272 次 |
| 最近记录: |