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
2010-04-08 09:48:11
I understand the mediator pattern. But I'm confused on how will I allow external handler for the command.
Lawrence A. Contreras
2010-04-08 10:20:01
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
2010-04-09 22:23:57
A:
I would consider using Controllers for the mediation between the ViewModels. The WPF Application Framework (WAF) shows how this works.
jbe
2010-04-15 18:03:50