I’m biting off a big chunk here trying to learn MVVM, Unity, and Prism all at once (ack!). So far it’s gone reasonably well but I run into stumbling blocks now and then. One of them is this:
I have a VM that defines a master-detail screen. In the detail section, I want to divide the detail lines across multiple DataGrids on a tab control. Each DataGrid contains a distinct subset of the detail lines based on the value of a property in the line (“Section”). So essentially, I read my master entity which contains a collection of detail entities. And I think I need to expose different views of this collection to the View so that the DataGrids can each bind to the proper filtered subset of the details collection. The DataGrids have to be editable. I’ve made a few attempts at exposing various levels of CollectionViews as ItemsSources for the DataGrids but nothing seems to work properly. Also, I’m thinking it would probably be best to factor out the DataGrids into a generic View since they all display the same info (just over a different subset of details) but I’m not sure how to do that. Can anyone help?
Thanks,
Dennis
If I’m understanding you correctly, you have something like this:
Where
Detailsis a List that contains many different types of objects, and you want to display a different View (DataGrid) for each type of object?I would use a
TabControlwith it’sItemsSourcebound to theDetailsproperty, and then use aDataTriggerwithin the TabItem to determine how the ItemTemplate (tab content) should get drawn.Something along the lines of this:
That’s just a rough example which would change the TabItem’s template based on if the
Detail.ItemTypeis “Address” or “Phone”. I’ve also done this in the past with a Converter that checks the type of object instead of needing to have a Type property on the object, which works even better.If you’re interested, I wrote a brief article here that shows a few ways of switching Views or UserControls based on ViewModel data