ganeshran
ganeshran

Reputation: 3512

WPF MVVM - Refresh Treeview

I have created a Treeview in WPF MVVM (Project is using MVVM Light).

When I add a data to a DataTemplate of the Treeview, I want to reload the entire treeview. For this I am sending a Message from my data entry viewmodel to my treeview view model.

Though the message is recieved, the tree doesnt get refreshed. I called LoadChildren on each node again, and used RaisePropertyChanged as well but the data didnt get updated.

Upvotes: 1

Views: 6747

Answers (2)

Dean Chalk
Dean Chalk

Reputation: 20481

The TreeView is bound to a hierarchical collection in your ViewModel, and it has a property name, e.g. TreeViewData. Assuming your ViewModel implements INotifyPropertyChanged, simply call the PropertyChanged handler for property TreeViewData. This will completely refresh the TreeView.

Upvotes: 3

matfillion
matfillion

Reputation: 867

My answer is based on the fact you are using Mvvm light, since you did not provide code I will just state the obvious and it might solve your issues.

Is your viewmodel implementing the ViewModelBase interface? (which implements INotifiedPropertyChanged)

I never used TreeViews but assuming it's using a collection as its datatemplate, you have to call RaisePropertyChanged on the collection whenever you add/remove an item and also whenever an item changes. Calling RaisePropertyChanged only in the "set" of the collection won't be enough.

Upvotes: 1

Related Questions