6

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?

Eugene
  • 1,515
  • 1
  • 13
  • 23
  • 3
    Hi are you using any MessagingServices Like that of Galasoft or any Messaging to Communicate from ViewModel to View – yo chauhan Jul 16 '12 at 15:32

2 Answers2

3

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.

Rachel
  • 130,264
  • 66
  • 304
  • 490
1

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.

yo chauhan
  • 12,079
  • 4
  • 39
  • 58
  • If I understand you correctly your approach can work but it will be very difficult to manage a huge system. Could you please provide some code sample to help me understand. Thanks – Eugene Jul 17 '12 at 08:07