Soumya Kar
Soumya Kar

Reputation: 11

How to communicate between Parent Window and Child User Control using WPF MVVM

I am relatively new to WPF MVVM. I am currently working on a small project to understand the details around the concepts of WPF MVVM and am facing a small issue that I cannot get my head around.

I have a main window with a <ContentControl Content="{Binding Path=CurrentViewModel}" /> with the CurrentViewModel property being set on the main window ViewModel class. The main window has three button controls for listing, editing and removing an entity. The ContentControl loads a UserControl (ListEntities) which lists all the entities from some data store. The UserControl xaml has its own ViewModel class which is being set to the ContentControl content property and the view gets loaded using DataTemplates.

This is all working fine. My requirement is that when a user will select a row in the ListEntities UserControl GridView, the Edit and Remove buttons has to be enabled(using the CanExecute method of the ICommand infrastructure). The issue is that I can set a SelectedEntity property on the UserControl ViewModel class based on SelectedItem property binding, but how do I get the selected item to be available to the main window view model class so as to enable the edit/remove buttons(how from the Parent View would I get to know that an item in the Child View has been selected using the MVVM pattern). So in a more generic way, I am trying to understand the most standard procedure to enable communication between the independent ViewModels of the Parent window and child User Control based on some event in either the Parent or the Child view.

Upvotes: 0

Views: 1944

Answers (1)

silverfighter
silverfighter

Reputation: 6882

One a approach that I like to communicate between ViewModels is Publish and Subscribe. Most of the MVVM-Frameworks provide something like an EventAggregator or a Messenger

The pattern is that you pass in the Messenger via DI into the ViewModel (sometimes also useful for views in a decoupled way) and on one side you send the notification on the other side you subscribe. Pub-Sub.

The also implement nice things like passing paylod, typed messages and callbacks to inform the sender back. You should definitely investigate further on this topic.

Because it always starts with Parent Child and it will get fancy soon. Open Dialogs for loading files, etc ... So for my point of view PubSub would be the way to go, to get the best decoupling here. But handle with care too much can also end up confusing ...

Prism

Event Aggregator https://msdn.microsoft.com/en-us/library/ff921122(v=pandp.20).aspx

MVVM Light

http://dotnetpattern.com/mvvm-light-messenger (http://www.mvvmlight.net/help/WP8/html/9fb9c53a-943a-11d7-9517-c550440c3664.htm).

Reactive UI

https://reactiveui.net/docs/handbook/message-bus/

Caliburn Micro EventAggregator https://caliburnmicro.com/documentation/event-aggregator

Hope this helps (the links are just randomly sorted and not ordered by preference, so it's up to you =) ...)

Upvotes: 2

Related Questions