开发者

In wpf treeview, how to redraw the nodes with their previous expanded values(using MVVM)

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开发者_JAVA技巧 ??
  • 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


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.


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


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜