views:

405

answers:

3

Hi to all,

I am developing a wpf desktop app with strict MVVM pattern.
Current my app is doing following things:

  • Showing a Treeview with HierarchicalDataTemplate.
  • User can expand or collapse Nodes.
  • User can add add new Nodes(Drag n Drop + double click).
  • Everytime a new Node is added Model is updated and Treeview is recreated based on Model.
  • Because Treeview is recreated, all nodes are shown as expanded after adding nodes.

I want to show nodes with their previous expanded condition. Is there any way to do this using MVVM ? What I have thought so far is

  • Model should not contain any data related to how to draw UI ??
  • VM should just get data from Model and put it in UI(and pass date from UI to Model) ??

Thanks for your thoughts. I may be way far out form rail. But just want to have some wisdom from you guys.

Thanks
PAIJA

+1  A: 

Hi,

One solution what I think could be is to stop the recreation of the tree, just update the model and only add nodeitems to the current node where you are dropping them. Just refresh the collections in model and dont refresh the tree. Let us know if this does't suits your architect.

Thanks, Jagdev Josan

jagdev josan
Thank you for you suggestion. Yes I think that is one of the way. But I was just wondering if thats the correct way? What is bugging me is that there may be some mismatch in Model and UI at some time.Is it the general pattern everyone else follows? Or is this scenario not so general. Thanks.
Sant
there wont be a mismatch, because the two will be bound together. the ui will perfectly represent your view model. they will stay in sync.
Aran Mulholland
+1  A: 

If you haven't already, read this great article by Josh Smith: Simplifying the WPF TreeView by Using the ViewModel Pattern

Basically what he suggests there is to include a property called IsExpanded in your VM and bind the TreeView to it correctly so that the expanded/collapsed status is entirely controlled by the programmer.

Aviad P.
Thank you Aviad,I saw the article. I guess doing something similar is an viable option...Thank you very much.
Sant
A: 

The view model can contain view related information, that is what it is for. It is a bridge between pure business and pure view. My view models usually expose a few business properties of an object and add a few related view properties. If all you need is business properties, then bind straight to the business layer. Its only when you need to do something like your situation here that you need a view model.

If you want to completely recreate the tree (which sounds crazy) you can store the expanded state of the nodes in your view model and bind them to the tree view items using an ItemsContainerStyle. that way when you recreate your tree view your previously expanded nodes will still be expanded.

So your wrapped business objects will contain an extra property IsExpanded that you can use to restore your tree view state.

P.s. did i mention its a bit over the top to recreate the tree view?

Aran Mulholland
Thank you for your thoughtful opinion. So it seems that general opinion is like [jagdev josan] said, update Model and Treeview separately. I can understand why you hate the idea of "recreate the tree view"!! Thanks.
Sant