ViewModel<> Save in Silverlight

ViewModel<> Save in Silverlight

Old forum URL:

jh72i posted on Thursday, October 06, 2011

Hi folks,
I want to implement a grid-style interface whereby every cell is editable. I follow the MvvM pattern and all is fine except on Save. Whenever I trigger save its gets called for the individual item and not for the collection as a whole.

Any ideas how to sort this one out?











<CollectionViewSource x:Key="mainViewModelViewSource" d:DesignSource="{d:DesignInstance myVM:EditableCollectionVM, CreateList=True}" />
<CollectionViewSource x:Key="mainViewModelModelViewSource" Source="{Binding Path=Model, Source={StaticResource mainViewModelViewSource}}" />

...then a grid...

..then a button...and a trigger action:

 <Xaml:TriggerAction TargetControl="{Binding ElementName=SaveButton}" TriggerEvent="Click" MethodName="Save" />




class EditableCollectionVM : ViewModel<EditableCollection>



RockfordLhotka replied on Thursday, October 06, 2011

Set the DataContext on the TriggerAction control to point to your business object.

jh72i replied on Friday, October 07, 2011

Thanks Rocky.

A rookie mistake: I had forgotten that i needed to put the Path=Model on the collectionViewSource that grid was bound to. For anyone similarly struggling my trigger now simply looks like:
<Xaml:TriggerAction DataContext="{StaticResource mainViewModelViewSource}" TargetControl="{Binding ElementName=SaveButton}" TriggerEvent="Click" MethodName="Save" />

Rocky, I notice from the sample mentioned earlier that you also wrap access to the business objects in the collection in a viewmodel. I thought one of the reason for you exposing the Model property was so that the presentation can pick up all the good aspects of your rich as opposed to anaemic models(business objects). Is there a preferred (correct!) way of doing this?

Copyright (c) Marimer LLC