How to send parameters between view modes using viewmodellocator

I am building a WPF application using MVVMLight. I am using ViewModelLocator to create viewmodels. My problem is passing parameters between them.

For example, we have a situation:

We have a grid with some entities when one of them double-clicked. I need to create a new view with the details of this object. How to send id of selected item in ViewModel of new view?

+6
source share
2 answers

Usually you would use some kind of messaging system such as Prism EventAggregator or MVVM Light Courier .

Both systems remind me of the swap system: any part of the application can send messages and any part of the application and subscribe to listen to messages.

Thus, your DoubleClick team will translate the LoadItemMessage containing the selected Id element, and everything that is responsible for showing the element will subscribe to receive LoadItemMessages and will load the element every time it hears LoadItemMessage .

If you're interested, I have a short article about my blog about Linking View Modes with MVVM , which gives a high level overview of system events.

+3
source

This is a problem with ViewModelLocator (pass ViewModel parameters from View xaml). What you can do is create a property parameter of the Type object or (of type your SelectedItem) in the ViewModelLocator class. Bind this to the selected element of your grid, and then pass it to the new ViewModel. I hope this helps. NOte: If you create a property of an object of a type, remember to drop it.

+1
source

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


All Articles