gpt4 book ai didi

wpf - 针对 INotifyCollectionChanged 类型的数据绑定(bind)

转载 作者:行者123 更新时间:2023-12-03 10:14:11 24 4
gpt4 key购买 nike

我想要一个域模型集合类和一个 View 模型类,它们只反射(reflect)和投影域模型的元素——但它本身是只读的。

我的想法是这两种类型都实现了 INotifyCollectionChanged 接口(interface),然后 View 模型类型只是充当代理和投影仪,将模型元素包装在元素 View 模型类型中。

问题是:实现 INotifyCollectionChanged 是否足以使 WPF 数据绑定(bind)到例如DataGrid 或 ListView 或启用基于集合的数据绑定(bind)所需的最小接口(interface)集是什么?

最佳答案

INotifyCollectionChanged如果集合中元素中的属性发生更改,则不会更新您的 UI,仅当从您的集合中添加或删除整个元素时。

因此,如果您乐于跟踪整个元素的变化,那么 INotifyCollectionChanged任何进一步的粒度就足够了,您需要实现 INotifyPropertyChanged在您的属性 setter 中。

另一点值得注意的是,如果您使用 ObservableCollection容纳您的列表,这已经实现了INotifiyCollectionChanged为你。

编辑:

以下是微软的看法;

You can enumerate over any collection that implements the IEnumerable interface. However, to set up dynamic bindings so that insertions or deletions in the collection update the UI automatically, the collection must implement the INotifyCollectionChanged interface. This interface exposes an event that should be raised whenever the underlying collection changes.

WPF provides the ObservableCollection(Of T) class, which is a built-in implementation of a data collection that exposes the INotifyCollectionChanged interface.

Note that to fully support transferring data values from source objects to targets, each object in your collection that supports bindable properties must also implement the INotifyPropertyChanged interface.

Before implementing your own collection, consider using ObservableCollection(Of T) or one of the existing collection classes, such as List(Of T), Collection(Of T), and BindingList(Of T), among many others. If you have an advanced scenario and want to implement your own collection, consider using IList, which provides a non-generic collection of objects that can be individually accessed by index and thus the best performance.



从..

http://msdn.microsoft.com/en-us/library/ms752347.aspx#binding_to_collections

关于wpf - 针对 INotifyCollectionChanged 类型的数据绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15260240/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com