ObservableCollection<>与List<> [英] ObservableCollection<> vs. List<>

查看:85
本文介绍了ObservableCollection<>与List<>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多实体,每个实体中都有嵌套的 List<>



例如,我有具有 List< ColumnEntity> BaseEntity
ColumnEntity 类具有 List< Info> 等。



我们正在使用 WPF 用户界面,我们需要跟踪所有更改在 BaseEntity 的每个列表中。通过基于所需列表实例化新的ObservableCollection 并绑定到该 ObservableCollection 来实现。



将所有这些嵌套的列表更改为 ObservableCollections ?因此,我们可以跟踪 BaseEntity 本身的所有更改,而无需将 BaseEntity 的每个列表重新分配给修改后的绑定假设没有使用特定于 List 的方法。



ObservableCollection



>

解决方案

有趣的问题,考虑到List和 ObservableCollection 都实现了 IList< T> 并没有太大区别, ObservableCollection 还实现了 INotifyCollectionChanged 接口,该接口允许WPF绑定到它。



主要区别之一是 ObservableCollection 没有 AddRange 方法,这可能会带来一些影响。



我也不会在我知道的地方使用 ObservableCollection 由于这个原因,我不会拘束于此,重要的是要仔细检查您的设计,并确保您采用正确的方法来分离关注的层次。



关于 Collection< T> List< T> 之间的差异,您可以在这里查看
通用列表与集合


I have lots of entities with nested List<> in each.

For example, I have BaseEntity which has List<ColumnEntity>. ColumnEntity class has List<Info> and so on.

We are working with a WPF UI, and we need to track all changes in every List of BaseEntity. It is implemented by instantiating a new ObservableCollection based on the needed list, and with binding to that ObservableCollection.

What are the pros and cons changing all these nested Lists to ObservableCollections? So we can track all changes in BaseEntity itself without reassigning each list of BaseEntity to modified bound ObservableCollection?

Assuming that methods specific to List are never used.

解决方案

Interesting question, considering that both List and ObservableCollection implement IList<T> there isn't much of a difference there, ObservableCollection also implements INotifyCollectionChanged interface, which allows WPF to bind to it.

One of the main differences is that ObservableCollection does not have AddRange method, which might have some implications.

Also I would not use ObservableCollection for places where I know I would not be binding to, for this reason it is important to go over your design and making sure that you are taking the correct approach in separating layers of concern.

As far as the differences between Collection<T> and List<T> you can have a look here Generic Lists vs Collection

这篇关于ObservableCollection&lt;&gt;与List&lt;&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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