C# – MVVM with TreeView – add nodes

cmvvmnettreeviewwpf

How to add nodes to the tree (i.e. add new child to a specific node in the tree during runtime) in this example of TreeView?

Should the new nodes be added to
(a) the model or to
(b) the view-model?

How changes to one of them effects the other?

Best Answer

They should be added to the ViewModel, which will add them to the model (to keep consistency between the model and the ViewModel). In the ViewModel, the collection of child nodes should be an ObservableCollection<T>, so that the UI is notified when a node is added/removed