How should I communicate between ViewModels? How should I communicate between ViewModels? wpf wpf

How should I communicate between ViewModels?


Did you try to communicate via your model? I was not able to read your topic until the end but this is how I communicate between ViewModels.Both View Models have the instance of session.

public ViewModel1(ISession session)        {            _session = session;                   }public ViewModel2(ISession session)        {            _session = session;                   }

This way, when you test your application in BDD (behavior driven development), you can test your application without the view. The glue is the model.

As you can see on this picture, you should be able to test your application without the view.enter image description here


I came across the same situation where two view model is communicating each other. I have used Microsoft PRISM framework to publish and Subscribe.

In your case CustomerViewModel is parent View and ViewAllCustomersViewModel is child view.

  1. Download prism framework "Microsoft.Practices.Prism.PubSubEvents.dll" from https://www.nuget.org/packages/Prism.PubSubEvents/

  2. Add prism reference to your project "Microsoft.Practices.Prism.PubSubEvents.dll"

  3. Create some custom class which is used for communication modem.

      class Notifications : PubSubEvent<string>  {  }
  4. Create IEventAggregator eventAggregator singleton instance for your project and initialize it.

      public sealed class SessionInfo  {        public  IEventAggregator eventHanlder;        private SessionInfo (){        }        private static SessionInfo _instance = null;        public static SessionInfo Instance{                get{                 lock (lockObj){                  if (_instance == null) {                    _instance = new SessionInfo ();                    _instance.eventHanlder= new EventAggregator();                   }               }                  return _instance;               }             }            }
  5. Go to Popover model (ViewAllCustomersViwModel) button events handling and below codes in it.Now it has been published.

In ViewAllCustomersViwModel.cs:

      public void OnSelectedItem(Item item)     {            SessionInfo.Instance.eventHanlder.GetEvent<Notification>().Publish(item.id);      }
  1. These event aggregator has to be subscribe where it is needed. So add below code on your Parent View model (CustomerViewModel)

CustomerViewModel.cs

       public class CustomerViewModel      {               public CustomerViewModel()              {                  SessionInfo.Instance.eventHanlder.GetEvent<Notifications>().Subscribe(OnReceivedNotification);               }        //Handling the notification     public void OnReceivedNotification(string itemId)        {            Debug.WriteLine("Item Id is :" + itemId);        }     }

For more information:

https://sites.google.com/site/greateindiaclub/mobil-apps/windows8/communicationbetweenviewmodelsinwindows8mvvmpattern


I believe that standard way is to pass it through View.Depending on how you instantiate your views, it could be DependencyProperty to bind in XAML, constructor parameter, or anything else.Then View passes it to it's ViewModel (pushes it to VM, not the way around: ViewModel should not be aware of View). This way you get a standalone closed component (your View), and external code does not know about it's internal implementation (which is ViewModel).

In XAML it can be something like

<ListBox x:Name="customers" /><CustomerView Customer="{Binding SelectedItem, ElementName=customers}" />

And then in CustomerPropertyChanged handler you push value to the ViewModel.