问题
I have ListView which I am binding to a Dictionary collection, but new new items are added to the collection, the listview is not updating. How this can be achieved in WPF?
回答1:
Both sides of WPF binding should support INotifyCollectionChanged interface in order to notify about collection changes. Dictionary does not support it. So you can either use ObservableCollection class (it does not provide functionality like search a value by associated key), or you can create you own ObservableDictionary class which implements both IDictionary and INotifyCollectionChanged interfaces
PS: If you need bind both keys and values of the Dictionary - you've to implement own class, if it is enough displaying either Keys or Values - simply create ObservableCollection based on dictionary.Keys or dictionary.Values.
Useful links:
- ObservableCollection Class
- How to: Implement the INotifyPropertyChanged Interface
来源:https://stackoverflow.com/questions/8311989/update-listview-when-collection-datasource-updates