How many matches fit between ViewModels in MVVM

I am developing a fairly simple WPF application to display a hierarchy of objects plus the detail of a selected object using the UserControl wrapping a TreeView in the left pane (a tree control) and another UserControl wrapping a ListView / GridView on the right (a part control).

Tree management uses MVVM after this article by Josh Smith quite closely and has several types of ViewModel, all of which are based on the same basic type, TreeViewModel . The main window is configured using the MainWindowViewModel , as in this article by Josh Smith , and provides the TreeViewModel used to populate the first generation of the tree.

However, when I want to populate the details panel on the right, I have a problem that the SelectedItem of the tree control is obtained from the TreeViewModel when I need a completely different type of ViewModel for the details panel, which will expand the object into the property / value table using reflection.

So the questions are:

  • Is it possible for MainWindowViewModel display a TreeViewModel for a control tree? I believe the answer here is yes, but I am open to suggestions to the contrary.

  • How to select the selected item in the control tree with the right-click ViewModel for the details pane? One option, apparently, is that MainWindowViewModel keeps track of the selected item in the tree and performs the adaptation, exposing it as another property, but I'm not sure if there is a better solution.

I am new to WPF and the MVVM pattern, so please excuse the rather basic point of the question. I did a small part of reading against the background of the template, looked at some sample applications, etc., but I can’t find anything concrete enough so that I can be sure of the answer. I also understand that MVVM can be excessive for the application, it's simple, but I use it partially as a training exercise.

+4
source share
3 answers

1.If it would be appropriate for the MainWindowViewModel to display the TreeViewModel for tree management?

I believe so. The model should hide the look from FOR THE LOOK logic, but it cannot hide the tink, like a logical structure.

2. How should I select the selected item in the control tree on the right Type ViewModel for the details pane? One option is that MainWindowViewModel keeps track of the selected item in the tree, and adapt by exposing it as another property, but I'm not sure if there is a better solution.

IMHO not.

+1
source

Working on a similar problem, I came to the conclusion that

 object Tag { get; set; } 

the property is inevitable :( except, perhaps, for some rare situations (only 1 type of objects in the whole tree structure).

0
source

However, when I want to populate the details panel on the right, I had a problem that the SelectedItem of the tree control is obtained from the TreeViewModel when I need a completely different type of ViewModel for the details panel, which will expand the object into the property / value table using reflection.

If you are really very concerned about this, you can create a higher order model class that provides two different properties - one of the TreeViewModel types and one of the DetailsViewModel types. Then the main window model will expose the same object for both the tree control and the part control, but the logical structure of the two types of views will be separated from each other.

Logically, the selected item in the tree control and the item that is displayed in the part control are the same. While the data control does not contain information about the parent / child relationships, and the tree control does not contain information about the name / value pairs of the thing, it is still the same. There is probably no need to worry too much about the fact that one object representing an object provides a property that uses only one kind of this thing.

0
source

All Articles