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

gridviewcolumnmvvmrefreshtreeviewwpf

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

Best Answer

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.

Related Topic