How to properly bind datagrid and textboxes?

Jan 13, 2013 at 1:57 PM
Edited Jan 13, 2013 at 2:12 PM

Hi,

This is my first time posting in CodePlex and just started using Simple MVVM Toolkit.

I have WPF app and having trouble with binding. My UI looks like this.

Basically 1 Product can have many Pricing Details. This screen is for the Edit mode where the information is retrieved from the database (for the sake of this sample, I just hard coded the values). Selecting an item in the datagrid will load the information on its respective textboxes. This is currently working fine. The only thing that I would like to modify is the behavior when changing the value in  the textbox because the datagrid gets immediately updated. Would it be possible to only update the datagrid once the EDIT button is clicked? Note that any changes made in this point is not yet persisted in the database, it would only be persisted when the UPDATE button(on the top) is clicked.

Here's the snippet of the XAML file:

DataGrid

<DataGrid AutoGenerateColumns="False" AlternationCount="2" Height="100" ItemsSource="{Binding ProductPrices, Mode=TwoWay}" SelectedItem="{Binding ProductPrice}" IsReadOnly="True" SelectionMode="Single" GridLinesVisibility="Horizontal">
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding UnitPrice, StringFormat=\{0:N\}}" Header="Unit Price" Width="100" />
        <DataGridTextColumn Binding="{Binding LowestPrice, StringFormat=\{0:N\}}" Header="Lowest Price" Width="100"/>
        <DataGridTextColumn Binding="{Binding EffectiveDate, StringFormat=\{0:d\}}" Header="Effective Date" Width="150"/>
    </DataGrid.Columns>
</DataGrid>

TextBoxes

<TextBox Grid.Column="1" Grid.Row="0" Text="{Binding Path=ProductPrice.UnitPrice, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True, ValidatesOnDataErrors=True, ValidatesOnExceptions=True, StringFormat=\{0:N\}}" Width="100" />
<TextBox Grid.Column="3" Grid.Row="0" Text="{Binding Path=ProductPrice.LowestPrice, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True, ValidatesOnDataErrors=True, ValidatesOnExceptions=True, StringFormat=\{0:N\}}" Width="100" />
<DatePicker Grid.Column="1" Grid.Row="1" Text="{Binding Path=ProductPrice.EffectiveDate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" Width="100" />

UpdateSourceTrigger=PropertyChanged must be the culprit. But I still don't have an idea on how to solve this case.

The source code is uploaded on SkyDrive:

https://skydrive.live.com/#cid=974337211B5F9E8B&id=974337211B5F9E8B!109

I'm open for any suggestions and it would be highly appreciated.

Thanks in advance!