6
votes

I am creating a WPF application using MVVMLight. I use 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 is doubleclicked I need to create a new view with details of that entity. How to send the selected item id to ViewModel of the new View?

2
Hi are you using any MessagingServices Like that of Galasoft or any Messaging to Communicate from ViewModel to View - yo chauhan

2 Answers

3
votes

Typically you would use some kind of messaging system, such as Prism's EventAggregator or MVVM Light's Messenger.

Both systems remind me of a paging system: any part of the application can broadcast messages, and any part of the application and subscribe to listen for messages.

So your DoubleClick command would broadcast a LoadItemMessage containing the selected item Id, and whatever is responsible for showing the item would subscribe to receive LoadItemMessages and would load the item whenever it hears a LoadItemMessage.

If you're interested, I have a brief article on my blog about Communication between Viewmodels with MVVM that gives a high level overview of event systems.

1
votes

That is a problem with ViewModelLocator (to pass the parameters to ViewModel from View xaml). What you can do is Create a Property Parameter of Type object or (of type your SelectedItem) in ViewModelLocator class. Bind this to the SelectedItem of your Grid and then pass it to your new ViewModel. I hope this will help. NOte: If you create the property of type object don't forget to cast it.