views:

50

answers:

3

I have a UserControl UserControl1 and a button inside the UserControl1. And I have a UserControl1ViewModel that has an ICommand property for the button. Using this command I need to call a method outside(from other VMs or VM of the MainWindow) the VM. What is the best practice for this?

A: 

You're most likely looking to implement the Mediator pattern to handle the communication between two viewmodels.

Another SO question along the same vein is: mvvm-view-model-view-model-communications

Graeme Bradbury
I understand the mediator pattern. But I'm confused on how will I allow external handler for the command.
Lawrence A. Contreras
A: 

You might want to examine MVVM lite by Laurent Bugnion http://www.galasoft.ch/mvvm/getstarted/

This is a lightweight toolkit for helping enforce mvvm concepts. In it, every viewmodel is a static member in a ViewModelLocator class. So for instance, in your command you could do something like this.

ViewModelLocator.MainViewModel.MainContent = NewContent;

You can totally do this without mvvm lite, but using it really helps speed up the learning curve and enforce modularity.

matheeeny
A: 

I would consider using Controllers for the mediation between the ViewModels. The WPF Application Framework (WAF) shows how this works.

jbe