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

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

问题描述

我将List<Person> collection用作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<Person>集合.我将其更改为ObservableCollection<Person> collection.现在,只要集合发生更改,它就会更新视图.

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<Person>更新?有人吗?

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

推荐答案

请在名称中输入.一个简单的List不会告诉ui更新,换句话说就是视图无法观察到该列表".数据绑定背后没有神奇的魔术. WPF和DataBinding需要数据模型来告诉他们这是新的"或这已更改",您可能已经看到

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天全站免登陆