Right way to interact between ViewModels MVVM

2

I am building a WPF application with the MVVM design pattern.

I am using DataTemplates to load user controls within a ContentControl:

<DataTemplate DataType="{x:Type menu:MenuViewModel}">
   <menu:MenuView/>
</DataTemplate>


<ContentControl Content="{Binding Menu}" Visibility="{Binding MenuVisibility, 
        Converter={StaticResource BooleanToVisibility}}"/>

In addition to the menu I have a user control to manage the StatusBar and this is where the doubt arises. So that the other ViewModels can interact with the StatusBar, what I have done is create a MainViewModel in each ViewModel that needs this functionality and then assign it from the MainViewModel as follows:

_ChatViewModel.mainViewModel = this;
CurrentViewModel = _ChatViewModel;

This works, but I think there must be a "wheel" invented and I'm complicating. Is there any more "simple", "standard" mode that is being overlooked?

    
asked by Darío Alonso 08.02.2016 в 20:59
source

1 answer

0

To communicate viewmodel you should use events, you could apply the pattern Publish/Subscribe .

The MainViewModel exposes an event (in your case to update the statusbar), and those who want to report a change simply launch the same event respecting the signature and sending the arguments.

In this way you unlink the communication.

Communication Between Views in MVVM (Pub-Sub Pattern )

If you apply a library these include this concept such as MVVM Light’s Messenger or Microsoft Prism’s EventAggregator .

Communication between ViewModels with MVVM

    
answered by 08.02.2016 / 21:13
source