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 corre开发者_StackOverflow社区ctly, in the same way is does when using a standard DataGridTextColumn?
Thanks
Maybe it's because you try to make a two-way-binding for the TextBlock in the non-edit Template:
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding BaseUnitCostValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" TextAlignment="Right"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
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.
It was problem in my business model.
I had to change my property from
Public Property BaseUnitCostValue As Decimal?
Get
Return _BaseUnitCostValue
End Get
Set(ByVal value As Decimal?)
If _BaseUnitCostValue <> value Then
_BaseUnitCostValue = value
Me.DataStateChanged("BaseUnitCostValue")
Me.DataStateChanged("TotalBaseUnitCostValue")
End If
End Set
End Property
To
Public Property BaseUnitCostValue As Decimal?
Get
Return _BaseUnitCostValue
End Get
Set(ByVal value As Decimal?)
If (Not _BaseUnitCostValue.HasValue) OrElse (Not value.HasValue) OrElse _BaseUnitCostValue <> value Then
_BaseUnitCostValue = value
Me.DataStateChanged("BaseUnitCostValue")
Me.DataStateChanged("TotalBaseUnitCostValue")
End If
End Set
End Property
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.
精彩评论