I’ve seen a few code examples that basically create a View-Model for the Master, and a View-Model for the Details. Then they connect the Master to a View with a DataGrid, then the selected item of the Master View-Model is bound to a SelectedItem property of type ObservableObject(of DetailViewModel), which is then sent over to the Detail View-Model, which is then bound to a detail view… Such as is recommended here. Or something like this
After reading this… http://blogs.msdn.com/b/adonet/archive/2011/03/08/ef-feature-ctp5-code-first-model-with-master-detail-wpf-application.aspx
This.. One Model Entity, Multiple Pages -> Multiple Views? Multiple ViewModels?
And this… Multiple ViewModels associated with a single view
I’m very confused:) Seems like the preferred Prism way of doing this is to have a separate ViewModel for master and detail though, in my cause I feel it will be quite a bit more work to do it that way.
It seems to me that if your using Entity Framework DbContext and are utilizing the .Local property for binding it would make more sense to use one View-Model for Master/Detail situations.
Binding to the DbContext or any of it’s properties directly would be a bad idea because it would expose the Model to the View which violates the idea behind MVVM. (The View knows the ViewModel, the ViewModel knows the Model).
In a master-detail scenario you have two different viewmodels with 2 different views each because they each have different roles.
Imagine you have a list of Products as your data model, each product has an id, a name and a price:
Also you have some sort of data model which contains a list of products:
Then your MasterViewModel’s role is to exposes the
Productslist of the ProductRepository model and to provide a way to switch to the detail view:The
ProductViewModel‘s sole responsibility is to present aProduct: