wpf DataGridCheckboxColumn 两种方式绑定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10651206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DataGridCheckboxColumn two way binding
提问by DanBrum
I am using the DataGrid from the WPF toolkit in .NET 3.5.
我在 .NET 3.5 中使用 WPF 工具包中的 DataGrid。
I have a datagrid column bound to a boolean property from my source object.
我有一个数据网格列绑定到我的源对象的布尔属性。
The checkbox is calling the boolean's properties get accessor correctly.
该复选框正在正确调用布尔值的属性获取访问器。
However when checking or unchecking the box the get instead of the set is being called.
但是,当检查或取消选中框时,正在调用GET而不是集合。
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Object, Source={StaticResource model}, Mode=TwoWay}">
<DataGrid.Columns>
<DataGridCheckBoxColumn Binding="{Binding BoolProperty, mode=TwoWay}"/>
</DataGrid.Columns>
</DataGrid>
When I instead use a DataGridTemplateColumn with a Checkbox in it the property is set correctly however then it is more complicated to create a nice layout.
当我改为使用带有复选框的 DataGridTemplateColumn 时,该属性设置正确,但是创建漂亮的布局会更加复杂。
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding BoolProperty, Mode=TwoWay}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
What am I doing wrong using the DataGridCheckBoxColumn?
我在使用 DataGridCheckBoxColumn 时做错了什么?
采纳答案by H.B.
In a DataGrid
the bindings do not get committed until you end editing of the row/cell. If you press enter the binding will apply back to the source.
在DataGrid
您结束编辑行/单元格之前,绑定不会被提交。如果您按回车键,则绑定将应用回源。
Using a template like this overrides that behaviour, i wouldn't recommend that though. Also setting TwoWay
explicitly should not be necessary.
使用这样的模板会覆盖该行为,但我不建议这样做。TwoWay
也不需要明确设置。
回答by MoonHunter
I got same problem with you ,here is my solution
我和你有同样的问题,这是我的解决方案
<CheckBox HorizontalAlignment="Center" IsChecked="{Binding BoolProperty, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
回答by Marek Pavelek
My solution was to set UpdateSourceTrigger to PropertyChanged.
我的解决方案是将 UpdateSourceTrigger 设置为 PropertyChanged。
<DataGridCheckBoxColumn Header="Bool property" Binding="{Binding BoolProperty, UpdateSourceTrigger=PropertyChanged}"></DataGridCheckBoxColumn>
回答by Gennadij
My solution was to add ElementStyle with Style TargetType="CheckBox":
我的解决方案是添加带有 Style TargetType="CheckBox" 的 ElementStyle:
<DataGridCheckBoxColumn Binding="{Binding BoolProperty, UpdateSourceTrigger=PropertyChanged}">
<DataGridCheckBoxColumn.ElementStyle>
<Style TargetType="CheckBox"/>
</DataGridCheckBoxColumn.ElementStyle>
</DataGridCheckBoxColumn>