列表<>集合不会更新 MVVM 中的视图 [英] List<> collection does not update the View in MVVM

查看:10
本文介绍了列表<>集合不会更新 MVVM 中的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了Listcollection 作为 DataGrid 控件的 ItemsSource.

I used the List<Person> collection as the ItemsSource for the DataGrid control.

但如果我从列表集合中删除项目,它不会更新视图.我为解决方案苦苦挣扎了很长时间.

But it did not update the View if i remove the item from the List collection. I was struggling for a long time for the solution.

然后代替我的 ViewModel 中的 List 集合.我把它改成了 ObservableCollection;收藏.现在,只要集合发生变化,它就会更新视图.

Then Instead of the List<Person> collection in my ViewModel. I changed this into ObservableCollection<Person> collection. Now it updates the view when ever there is a change in the collection.

我不知道为什么它只更新 ObservableCollection ?有人吗?

I am not sure why it updates only for ObservableCollection<Person> ? Anyone ?

推荐答案

正如其名.一个简单的 List 不会告诉 ui 更新,换句话说视图无法观察列表".数据绑定背后没有奇怪的魔法.WPF 和 DataBinding 需要数据模型来告诉他们这是新的"或这是更改的",您可能已经看到 INotifyPropertyChanged, INotifyCollectionChanged 与集合相同,List<> 没有实现它,ObservableCollection 确实如此.

Well its in the name. A simple List doesn't tell the ui to update, in other words "the view can't observe the list". There is no weird magic behind Databinding. WPF and DataBinding needs the Data model to tell them "this is new" or "this is changed", you propably already saw INotifyPropertyChanged, INotifyCollectionChanged is the same but for collections, and the List<> doesn't implement it, ObservableCollection does.

这篇关于列表&lt;&gt;集合不会更新 MVVM 中的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆