Composite ViewModels in a WPF Application

I came to the following situation:

I have 2 view models and one view that contains 2 user controls to which view models are attached. The first virtual machine is a search function that returns a list of faces, and the second virtual machine is a more detailed description of each person.

I want to do the following:

public CompositeVM { public SearchVM SearchViewModel{get;set;} public DescriptionVM DescriptionViewModel{get;set;} } 

As I said, the view model also includes a list of found faces, so I wish DescriptionVM to be updated accordingly when choosing a person.

How can I achieve this type of interaction between virtual machines? Should I set the SelectedPerson property in SearchVM and pass it to DescriptionVM when the selected list item changes (quite a high connection with me)? Is there a simpler approach to this?

+4
source share
2 answers

It is possible to << 20> subscribe to the SearchViewModel PropertyChanged event and set DescriptionViewModel.SetSelectedPerson(SearchViewModel.SelectedPerson) .

There is no connection between SearchVM and DescriptionVM , since they do not know about each other. CompositeVM knows both of them, and is also responsible for their interaction.

+4
source

Alternatively, you can use the observer proxy template, for example, the Messenger class in MVVM Light:

http://blog.galasoft.ch/archive/2009/09/27/mvvm-light-toolkit-messenger-v2-beta.aspx

+1
source

Source: https://habr.com/ru/post/1400973/


All Articles