Wpf 4 DataGrid 列绑定问题
我创建了一个数据网格,其中的列绑定到可观察的集合。除了绑定到我的业务对象中可为空的十进制属性的列之外,一切都运行良好。
但是,如果我用作
<DataGridTextColumn Binding="{Binding Path=BaseUnitCostValue}" Header="Unit Cost Value" MinWidth="100" />
我的列定义,一切都很好,因为我最终希望它成为一个复杂的列,我尝试使用 DatagridTemplateColumn 这样,
<DataGridTemplateColumn Header="Unit Cost Value" MinWidth="100">
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox Text="{Binding BaseUnitCostValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding BaseUnitCostValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" TextAlignment="Right"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
但是,使用此列配置,尽管我可以在完成后立即输入一个值对单元格进行编辑,其值消失。
我还尝试使用转换器转换为字符串,然后再次转换为可为空的小数,但没有成功。
我强烈怀疑这与它绑定到可为空的小数这一事实有关。我是否需要对 cellTemplates 做更多的事情,以便正确绑定值,就像使用标准 DataGridTextColumn 时一样?
谢谢
I have created a datagrid with columns bound to an observable collection. All works well except for a column which is bound to a nullable decimal property from my business object.
If I use
<DataGridTextColumn Binding="{Binding Path=BaseUnitCostValue}" Header="Unit Cost Value" MinWidth="100" />
as my column definition all works well, however, as I will eventually want it to be a complex column I tried using a DatagridTemplateColumn such that
<DataGridTemplateColumn Header="Unit Cost Value" MinWidth="100">
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox Text="{Binding BaseUnitCostValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding BaseUnitCostValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" TextAlignment="Right"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
However, using this the column configuration, although I can enter a value as soon as I finish the edit to the cell, its value disappears.
I've also tried using converters to convert to a string and back again to a nullable decimal but with no luck.
I strongly suspect that this is something to with the fact that it's bound to a nullable decimal. Is there something more I need to do to my cellTemplates so that the value binds correctly, in the same way is does when using a standard DataGridTextColumn?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可能是因为您尝试在非编辑模板中对 TextBlock 进行双向绑定:
尝试将其更改为单向,可能会起作用。还删除触发语句。
如果它没有帮助,请查看输出窗口,如果您看到相关消息。
Maybe it's because you try to make a two-way-binding for the TextBlock in the non-edit Template:
Try to change it to one-way, may it works then. Remove also the Trigger-statement.
If it does not help, look in the output-window, if you see a related message.
这是我的商业模式的问题。
我不得不将我的财产从
To
更改为 问题就消失了。感谢 HCL,因为简单转换器中断点的想法向我表明该属性实际上从未被更新。
It was problem in my business model.
I had to change my property from
To
And the problem went away. Thanks to HCL as the idea of the breakpoint in a simple converter showed me that the property was never actually being updated.