views:

130

answers:

2

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

+1  A: 

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.

HCL
Unfortunately removing the mode=TwoWay and UpdateSourceTrigger=PropertyChanged from the textblock makes no difference
And you don't have a related message in the output-window?
HCL
a) Try what happens if you declare the TextBox as the default-template and you don't declare the edit-template. b) Create and attach a value-converter that does nothing than returning the input values. Set a breakpoint in the converter to see what happens.
HCL
Nope, there is nothing in the output window either. If I change the bound property to be a decimal rather than nullable decimal everything works fine with the above celltemplates, but I need the property to be nullable.
The value persists, but the converter is never called?. <DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBox Text="{Binding BaseUnitCostValue, Mode=TwoWay, Converter={StaticResource tempconverterkey}}" TextAlignment="Right"/> </DataTemplate></DataGridTemplateColumn.CellTemplate>
A: 

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.