Relationship between two presentation models in WPF MVVM

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

I have:

  • MainViewModel
  • ChildViewModel1
  • Childviewmodel2

Each time a property changes in MainViewModel, ChildViewModel1 and ChildViewModel2 should be notified.

Can anyone suggest a workaround?

EDIT: I think in the solution descrided MVVM Light ( http://simplemvvmtoolkit.codeplex.com/SourceControl/changeset/view/23821#313594. ) That implements the message bus. The right approach?

+7
source share
3 answers

In most cases, I would not suggest using a centralized place to exchange “events” / “notifications”, such as EventAggregator, etc. This leads to later issues related to obscure relationships between ViewModels. Such notifications make sense in very specific cases where the relationship between the listener / publisher is unknown even at the design stage. I would suggest drawing a simple diagram with the relationships between ViewModels and finding a way to use standard .NET events, so when you have clear relationships between ViewModels, for example ViewModel1, has a link to ViewModel2, so you can subscribe to the event or provide your own callback, therefore It will be easy to create such event notifications.

+11
source

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

+2
source

All Articles