Update ListView when Collection datasource updates

不羁岁月 提交于 2021-02-05 07:34:24

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!