This project has moved and is read-only. For the latest updates, please go here.

modeling a tree view

Oct 16, 2010 at 6:09 PM


i am in the process of learning the programming paradigm used in WAF. one problem that i am not sure how to solve correctly is interacting with the treeview control.

i am using the approach outlined here:

this article uses a view model for the whole tree and separate view models for the tree items.

two questions:

  1. i am tying to translate this approach to WAF and i am not sure how to implement the view model for items because items do not have corresponding views and view model needs its view as a parameter. should i use the Model type for that?

  2. yet another question about how to fetch the selected item from the tree view.

i see two ways to do that:

one is to have a property on the main view model that exposes the currently selected item, and set this property when an item gets selected (see the article, there the items view model has "selected" bit). to do that the items need to know the main view model, so i need to pass it to every single item. or use some sort of messaging system. too complicated.

the other is to ask the view to return the selected item. so, in my view interface i will have a method that returns the view model for the selected item. i am not sure whether this violates the separation of concerns principle.

please help

thank you


Oct 18, 2010 at 8:06 PM
Edited Oct 18, 2010 at 8:09 PM

Hi Konstantin,

I try to answer your questions:

  1. I would use the Model class as base class for “ViewModels” which don’t have an associated View class. They are quite suitable for objects which are consumed by DataTemplates.
  2. Maybe it’s possible to bind the TreeView.SelectedItem property to the MainViewModel. Then it wouldn’t be necessary for the TreeItemModels to pass the information back to the MainViewModel.
  3. Your second idea which extends the view interface so that the ViewModel can ask for the selected item does not violate MVVM or the separation of concerns principle. The sample applications of WAF use this approach when the task cannot be accomplished via “simple” property or command binding.