0

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?

Lance
  • 2,774
  • 4
  • 37
  • 57

3 Answers3

0

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

Community
  • 1
  • 1
Graeme Bradbury
  • 3,693
  • 22
  • 29
  • I understand the mediator pattern. But I'm confused on how will I allow external handler for the command. – Lance Apr 08 '10 at 10:20
0

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
  • 1,714
  • 2
  • 17
  • 32
0

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

jbe
  • 6,976
  • 1
  • 43
  • 34