views:

54

answers:

3

I am designing a master/detail view. Currently, I have a user control (detail) in my main view, and both have thier own vm. On the one hand, I think there should only be one vm because the detail will never exist without the master. It would also be easier to handle the CRUD process in one vm because of their tight dependency on one another. On the other hand, they are separate entities, and having two smaller vm vs one large one seems more manageable. Any thoughts?

+1  A: 

For the given scenario I would've created two different views and bound it to the same viewmodel.

If you lazy load stuff from a database, it might be cleaner to implement two viewmodels .

hkon
I was also thinking I would be forced to make my viewmodels, if separate, dependant on each other, which doesn't seem right. Thanks!
steveareeno
If you have two views bound to one view model, what is the view model modeling?
Robert Rossney
A pragmatic way to look at it would be to say that the viewmodel facilitates certain state and command properties and the view may choose to utilize this functionality. Thus the view may choose to display some subset of the data, and hook up to a subset of the commands.I see nothing wrong with a view, utilizing only part of the viewmodel, if that's all it needs. But if you start to implement "master" specific commands in the viewmodel, it's starting to smell two view models.
hkon
A: 

You might have a look at the BookLibrary sample application of the WPF Application Framework (WAF) which shows how to implement a master/detail view with two different ViewModels.

jbe
I'll have a look. Thanks.
steveareeno
A: 

The view model is the model of the view. If you have two views, each has a view model. If the views are interdependent, the models will be too.

The wisdom of having a separate view model for detail items becomes apparent as the complexity of your detail items grows. For a simple example, imagine a hyperlink presenting a command in the detail view, which should be enabled if the detail item meets some kind of criteria. Where are you going to put the source of the hyperlink's command binding?

Robert Rossney