There are a couple ways you could tackle this. The way you've already got isn't 
too bad. The important thing is that the VM is still testable.
Another option (although I'd prefer yours) is to put an OnSaving() event on 
your ViewModel, such that when save gets called, it raises the event.
The view code behind could then respond to that event and execute the required 
method. Another advantage to this approach is that you could supply an e.Cancel 
if the view isn't validated yet.

Also I'd use a lambda over delegate keyword =)

Another way could be that you still have the command as before, however your 
view uses the click event. In the code behind, you call the required method, 
then call Execute() on the command on your view model.
This is a controversial approach because some people think that View == XAML 
and that we shouldn't touch the code behind. In my opinion the code behind is 
part of the view and responding to events is not a bad thing.
The important thing here is that the event is handled in code behind, and it 
still talks to the view model by a command.

Personally I would go for the latter option I provided out of all three because 
you aren't changing your view model in anyway because of a view related issue.
But that would also be a bit of personal preference perhaps.  ;)

Steven Nagy
Readify | Senior Consultant
M: +61 404 044 513 | E: [email protected] | B: azure.snagy.name


-----Original Message-----
From: [email protected] [mailto:[email protected]] On 
Behalf Of Adrian Hara
Sent: Friday, 30 July 2010 1:09 AM
To: ozWPF
Subject: RE: MVVM

I don't know how clean this actually is in this case, but you could have the 
view model execute a "reverse command" that the view hooks (through a trigger 
or behavior) and does the stuff on the control. This concept is implemnted in 
the nRoute framework, so you can take a look at that for details.

Hope this helps ;)

________________________________________
From: [email protected] [[email protected]] On Behalf Of 
ross [[email protected]]
Sent: Tuesday, July 27, 2010 6:15 AM
To: ozWPF
Subject: Re: MVVM

Speaking of MVVM.

Can anyone suggest a clean technique for the scenario where a 3rd party 
datagrid control needs to have a method called on it before saving the view 
model.

I have a command on a save button that executes the save on the view model.  
All of the data is bound to the view model.

However, to make it user friendly, the grid control needs to have the
EndEdit() method called on it to resolve the binding in case the user is still 
editing a cell when they clicked save.  The users can be trained to always 
click away from a cell, or hit <Enter> to end the edit, but of course I can't 
rely on that.

It feels a bit annoying to use code behind, but what I have started doing is 
the following :

View Model :

private Action _saveCallBack;
 public void SetupFinaliseUICallback(Action saveCallBack) {
        _saveCallBack = saveCallBack;
}


private void Save()
    {
            if (_saveCallBack != null)
                _saveCallBack();

           // final validation

          // persist the data
   }

View code behind:

 void LoadingMainView_Loaded(object sender, RoutedEventArgs e)  {
            ViewModel.SetupFinaliseUICallback(delegate()
            {
                    loadingsGrid.EndEdit();
            });
 }
_______________________________________________
ozwpf mailing list
[email protected]
http://prdlxvm0001.codify.net/mailman/listinfo/ozwpf
_______________________________________________
ozwpf mailing list
[email protected]
http://prdlxvm0001.codify.net/mailman/listinfo/ozwpf

_______________________________________________
ozwpf mailing list
[email protected]
http://prdlxvm0001.codify.net/mailman/listinfo/ozwpf

Reply via email to