简体   繁体   中英

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM