9

I am developing a WPF application and I have some problems to communicate one view model with another.

I have:

  1. MainViewModel
  2. ChildViewModel1
  3. ChildViewModel2

Every time a property changes in MainViewModel, ChildViewModel1 and ChildViewModel2 should get notified.

Can anyone suggest a workaround?

EDIT: I am thinking in a solution descrided MVVM Light (http://simplemvvmtoolkit.codeplex.com/SourceControl/changeset/view/23821#313594.), that is implementing a message bus. Is it the right approach?

guilhermecgs
  • 2,913
  • 11
  • 39
  • 69
  • Way too general. You need to try this on your own. See [INotifyPropertyChanged](http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx) – tnw Apr 22 '13 at 14:10
  • 1
    The title of this is misleading and there's not a specific question. – Big Daddy Apr 22 '13 at 14:14
  • 1
    I don't know about other MVVM frameworks, but MVVM Light and ReactiveUI include messages bus implementations as mentioned in your title. – kenny Apr 22 '13 at 14:19

3 Answers3

12

In most cases I would NOT suggest using any centralized place to share "events"/"notifications", like EventAggregator, etc.. This leads to later issues related with not clear relations between ViewModels. Such notifications makes sense in very specific cases when relations between listener/publisher is not known even on design stage. I would suggest draw simple diagram with relations between ViewModels and find a way of using standard .NET events, so when you have clear realtionships between ViewModels like ViewModel1 has a reference to ViewModel2 so can subscribe to an event or provide own callback, so it will be easy to build such event notifications.

sll
  • 61,540
  • 22
  • 104
  • 156
  • 2
    it is a good answer, but since my application is very simple, I will use a "centralized place to share "events"/"notifications", like EventAggregator" due to plenty of references and code examples – guilhermecgs Apr 22 '13 at 17:59
  • this is a very bad advice that would lead to horrible memory leaks very quickly. if you want to see relations more clearly you can create a resharper addon that will recongnize register/unsubscribe patterns – Nahum Jul 25 '16 at 06:39
  • This approach is much easier to debug than the accepted answer. – Josh Noe Jun 23 '17 at 19:28
  • Could you provide a very simple example of what you suggested? I need very simple thing to pass an object from one view model to the other. Can you help me? – Ehsan Aug 22 '19 at 18:41
6

the common way for viewmodels to communicate between themselves is implimentation of theMediator design pattern

here is how it is done in MVVMLight http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/27aaefff-e463-451c-87d9-37367a343e0e

in Prism is: http://blogs.u2u.be/diederik/post/2011/01/15/Using-the-Prism-40-Event-Aggregator.aspx

in Caliburn is: http://www.mindscapehq.com/blog/index.php/2012/02/01/caliburn-micro-part-4-the-event-aggregator/

Nahum
  • 6,959
  • 12
  • 48
  • 69
3

I would use a IService that is implemented by each view model. Then in the view models you can pass the service properties to properties of the view model that implement INotifypropertychanged. For example, I have a service called INavigationService that is implemented by my view models and it has properties like CanNavigate, currentView etc that I bind to in my view models. Changes to these properties can cause navigation or change properties that other view models are binding to.

J King
  • 4,108
  • 10
  • 53
  • 103
  • Could you provide a very simple example of what you suggested? I need very simple thing to pass an object from one view model to the other. Can you help me? – Ehsan Aug 22 '19 at 18:43