How to do Master/Detail with MVVM

How to do Master/Detail with MVVM

Old forum URL: forums.lhotka.net/forums/t/9478.aspx


c_manboy posted on Wednesday, September 08, 2010

I have successfully started a MVVM wpf app with a MainWindow containing a LeftContent and RightContent.  My TriggerAction fires successfully when the Window loads and loads the LeftContent with my GroupsView and the RightContent with my ReportsView.

I now want to filter my ReportsView based on the selected item in the GroupsView. I'm guessing that I need the GroupsView to set a property on my ReportsViewModel which can then call a ReportsList changed event on the model to trigger the filter. 

(My ReportsViewModel follows the SlDemo EmployeeItemListViewModel and EmployeeItemViewModel which uses a Public observablecollection(of EmployeeItemViewModel) to bind to).

RockfordLhotka replied on Wednesday, September 08, 2010

You are using Bxf - so LeftContent and RightContent are separate usercontrol objects?

I wouldn't approach the problem that way - it makes more work for you.

It is better to keep master-detail elements in a single usercontrol, because then data binding will do all the work for you.

When the user selects an item on the left, it will automatically restrict the detail list (on the right) to the selected item's details.

If you are spanning usercontrol objects, this gets much more complex - because you assume the responsibility for coordinating across the views and viewmodels instead of letting data binding do all the work for you...

c_manboy replied on Thursday, September 09, 2010

My Report object contains Name, Group, Frequency properties.  Where I'm struggling is in how to implement grouping on the collectionviewsource and display the groupdescriptions in listbox1 and subsequently filter listbox2 showing the reports in that groupdescription.

At the moment I got it to work with two user controls using TriggerAction.  LeftPanel displays the groups in a listbox from a BLB object with a TriggerAction on each listitem.  The TriggerAction then populates RightPanel with the Reports for that group.  I overloaded my factory method to accept a parameter to fetch the reports in that group.  The downside to this approach is that I'd like the user be able to determine the group, either by Group or by Frequency, which suggests that if I could make it work with a collectionviewsource then it'd be easier. 

The MVVM pattern is starting to click for me, so I think I can come up with single UC solution soon enough.

Thanks Rocky.

Copyright (c) Marimer LLC