Silverlight, MVVM, and datagrid not saving changes

1.3k Views Asked by At

I'm trying to use MVVM to display a datagrid and save the changes when the row is done being edited. Here is the XAML for the datagrid:

<data:DataGrid x:Name="discountsDataGrid"  ItemsSource="{Binding Discounts, Mode=TwoWay}"  MinHeight="200" AutoGenerateColumns="False" SelectedItem="{Binding SelectedDiscount, Mode=TwoWay}">
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="RowEditEnded">
                            <i:InvokeCommandAction Command="{Binding SaveChangesCommand}" CommandParameter="{Binding SelectedDiscount}"  />
                        </i:EventTrigger>
                    </i:Interaction.Triggers>

                    <data:DataGrid.Columns>
                        <data:DataGridTextColumn Header="Name" Binding="{Binding Name}" />
                        <data:DataGridTextColumn Header="Discount Amount" Binding="{Binding Amount}" />
                    </data:DataGrid.Columns>
                </data:DataGrid>

And here is the code that saves on the ViewModel:

public void Save(object parameter){
        if (ctx.HasChanges){
            ((IEditableObject)SelectedDiscount).EndEdit();
            ctx.SubmitChanges(SubmitOperation, null);
            RaisePropertyChange("Discounts");
        }
    }

Before I had the line '((IEditableObject)SelectedDiscount).EndEdit();' I would be an exception because no end edit was being called from the datagrid. Since I'm trying to stick to MVVM, the view model shouldn't know about the datagrid that is displaying the data. So, I added that line, the code gets executed, and the returning result shows that there was a modified entry and no validation errors occurred. But it never sticks to the database. Is there any reason why this code is not completely writing the values back to the database? The ctx variable is just a RIA Services domain service. Before going the MVVM route, I usually just called datagrid.CommitEdit(), but I shouldn't have access to it now and was hoping that the call to IEditableObject would work. Any ideas?

1

There are 1 best solutions below

0
On

On this way works for me, I'm using SL4 , mvvm toolkit 3 and WCF RIA Services, ds= domain service

private void guardarFila()
{
IsBusy = true;
ds.SubmitChanges(new Action<submitoperation>(SubmitChangesCompleted), null);
}
private void SubmitChangesCompleted(SubmitOperation args)
{
IsBusy = false;
}