tags:

views:

53

answers:

1

The following scenario came up in our project: We have hierarchical business objects. The root node is meant to be a project. The project contains different kind of data in it. This data is split up in "Static Data", "Result Data" and "Control Data".

Project
   |
   +--- Static Data
   |          |
   |          +---- Dataset 1
   |          |
   |          +---- Dataset 2
   |
   +--- Control Data
   |          |
   |          +---- Dataset 3
   |          |
   |          +---- Dataset 4
   |
   +--- Result Data
              |
              +---- Resultset

The application has a kind of Project Explorer (like Solution Explorer in VS) that shows the data structure as it is shown above in a tree view. To achieve that I create different view models with the corresponding models, e.g. ProjectViewModel, StaticDataViewModel and DatasetViewModel for the leaves. If the user clicks on one of the datasets a view opens that shows the data.

When I display the datasets in a other view, should I use the DatasetViewModel that is used in the tree, or should I create a new View Model (DatasetDetailsViewModel)?

Two things I read about the MVVM pattern: 1. The ViewModel should not be aware of how its data is displayed in the View. 2. The ViewModel is some kind of state machine.

Having those two points in mind I am not sure whether I could use the same ViewModel for tree view and details view. As on the one hand I could use the same view model, because it's just a different kind of displaying the dataset. The tree view is just showing the name of the dataset, whereas the detailed view shows the actual data that is contained. On the other hand, the view model is some kind of state machine, and the views may have states that do not fit together. For example if I have a visibility flag, I might set it to true for the treeview but in the detailed view I want to set to false.

So I am not sure if I should rather create a new view model or use the existing one from the treeview.

Opinions wanted!

Thanks, Florian

+1  A: 

I would probably leverage the hierarchical datatemplate in wpf see this article.

And basically it seems to me that you just need a TreeView control with its ItemSource or DataContext to ProjectViewModel and then create some other datatemplates for your other viewmodels.

See your ViewModel just needs to PROVIDE all the information for the view to display, it doesn't nor shouldn't care exactly how it gets displayed, or what technology displays it (e.g. WPF) . That's where MVVM is supposed to shine, it separates ui logic from the actual implementation of the ui.

DataTemplates are great because you can reuse sections of xaml. If you have a DataSetViewModel you can create a DataTemplate that will display that type a certain way without any further configuration on your side.

Everytime you want to create a new viewmodel you need to ask yourself, "Is there a viewModel that has all these properties already? If you answer yes, I wouldn't create a new ViewModel, less is more sometimes.

Hierarchical templates are good for usercontrols that have nested viewmodels

Jose
Thanks, Jose! Actually this is what I tend to do: Reusing ViewModels as often as I can. But in some cases, the ViewModel classes became very huge. This is because I had a ViewModel that almost provided all information. I extended the ViewModel with the information or commands that were missing. This makes me wondering if I am on the right way.And yes, I am alredy using datatemplates, especially the hierarchical one for the TreeView.
Florian