0

This UI consists of a Main.xaml which contains a navigation frame which holds (Person.xaml), so there's two different viewmodels involved.

enter image description here

I would like to filter the content of the datagrid when the menu in Main.xaml is clicked.

  • People (shows all)
  • Score above 50
  • Score below 50

My approach is to use the MVVM light Messaging by having PersonViewModel subscribe on a certain message which is sent from MainViewModel. This would work, but are there any other ways of doing this (best practice)?

I'd hate to implement the MvvmLight Messaging for all my scenarios where UI elements from different viewmodels have a need to communicate, if there is a better way of solving this.

Kman
  • 4,809
  • 7
  • 38
  • 62
  • lack of Interface in silverlight in the Routing Event backbone, this can be nicely implemented on WPF by passing `IInputElement` on the constructor of viewmodel, then use it to pass message ANYWHERE (view/viewmodel). see the idea here http://stackoverflow.com/a/11068208/212706 – ktutnik Jun 27 '12 at 06:05

3 Answers3

2

If you are using MVVM light, I guess you have created these viewmodels in the ViewModelLocator. So just use it to get access among viewmodels in your application.

Another approach is to use some type of IoC container like Unity or SimpleIoC.

I hope this helps you.

oarrivi
  • 191
  • 9
1

We use MVVM light also. From my understanding of your requirements. you can probably just use a single DomainContext and then loading all the entities to it and then querying the context for each click item.

once you have a Context, you can basically query it dynamically. Here's something from one of my code.

 GetUserBUGroups = SecurityDomainContext.Current.UserBUGroups.Where(ub => ub.UserID == GetUsers.UserID).OrderBy(o => o.BUGroup.BUGroupDesc).ToList();
stevenjmyu
  • 926
  • 4
  • 16
  • 31
  • Interesting way to solve the problem! I never thought of this as a possible solution :) – Kman Jun 27 '12 at 20:14
0

For simple scenarios such as this, it does not seem appropriate to use several ViewModels. I believe you should insert the filter options directly in the PersonViewModel

nicolaspanel
  • 943
  • 11
  • 21
  • It's just an example, the real application will have several views in the navigation:Frame, so the menu on the left will have to communicate with any of the viewmodels which the view in the navigation:Frame is binded to. – Kman Jun 27 '12 at 20:10