ObservableCollection<T> 类

定义

表示一个动态数据集合,在添加项、移除项或刷新整个列表时,此集合将提供通知。Represents a dynamic data collection that provides notifications when items get added, removed, or when the whole list is refreshed.

generic <typename T>
public ref class ObservableCollection : System::Collections::ObjectModel::Collection<T>, System::Collections::Specialized::INotifyCollectionChanged, System::ComponentModel::INotifyPropertyChanged
[System.Serializable]
public class ObservableCollection<T> : System.Collections.ObjectModel.Collection<T>, System.Collections.Specialized.INotifyCollectionChanged, System.ComponentModel.INotifyPropertyChanged
type ObservableCollection<'T> = class
    inherit Collection<'T>
    interface INotifyCollectionChanged
    interface INotifyPropertyChanged
Public Class ObservableCollection(Of T)
Inherits Collection(Of T)
Implements INotifyCollectionChanged, INotifyPropertyChanged

类型参数

T

集合中的元素类型。The type of elements in the collection.

继承
ObservableCollection<T>
派生
属性
实现

注解

在许多情况下,您使用的数据是对象的集合。In many cases the data that you work with is a collection of objects. 例如,数据绑定中的ItemsControl常见方案是使用ListBoxListViewTreeView等来显示记录集合。For example, a common scenario in data binding is to use an ItemsControl such as a ListBox, ListView, or TreeView to display a collection of records.

可以枚举实现IEnumerable接口的任何集合。You can enumerate over any collection that implements the IEnumerable interface. 但是, 若要设置动态绑定, 以便集合中的插入或删除操作UIUI自动更新, 该集合必须INotifyCollectionChanged实现接口。However, to set up dynamic bindings so that insertions or deletions in the collection update the UIUI automatically, the collection must implement the INotifyCollectionChanged interface. 此接口公开CollectionChanged事件,即每当基础集合发生更改时应引发的事件。This interface exposes the CollectionChanged event, an event that should be raised whenever the underlying collection changes.

WPFWPF提供类,该类是INotifyCollectionChanged实现接口的数据集合的内置实现。 ObservableCollection<T>provides the ObservableCollection<T> class, which is a built-in implementation of a data collection that implements the INotifyCollectionChanged interface.

在实现自己的集合之前, 请ObservableCollection<T>考虑使用或一个现有的集合类, List<T>例如、 Collection<T>BindingList<T>Before implementing your own collection, consider using ObservableCollection<T> or one of the existing collection classes, such as List<T>, Collection<T>, and BindingList<T>, among many others. 如果你有一个高级方案,并且想要实现自己的集合,请IList考虑使用,它提供可按索引单独访问的对象的非泛型集合。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. 实现IList可为数据绑定引擎提供最佳性能。Implementing IList provides the best performance with the data binding engine.

备注

若要完全支持将数据值从绑定源对象传输到绑定目标,则集合中支持可绑定属性的每个对象都必须实现相应的INotifyPropertyChanged属性更改通知机制,例如交互.To fully support transferring data values from binding source objects to binding targets, each object in your collection that supports bindable properties must implement an appropriate property changed notification mechanism such as the INotifyPropertyChanged interface.

有关详细信息,请参阅数据绑定概述中的 "绑定到集合"。For more information, see "Binding to Collections" in Data Binding Overview.

有关 XAML 使用情况的说明Notes on XAML Usage

ObservableCollection<T>在版本3.0 和3.5 中,可用作Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF)中的 XAML 对象元素。ObservableCollection<T> can be used as a XAML object element in Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF), in versions 3.0 and 3.5. 然而,使用情况却有很大的限制。However, the usage has substantial limitations.

  • ObservableCollection<T>必须是根元素,因为x:TypeArguments仅在根元素的对象元素上支持必须用于指定泛型ObservableCollection<T>的约束类型的特性。ObservableCollection<T> must be the root element, because the x:TypeArguments attribute that must be used to specify the constrained type of the generic ObservableCollection<T> is only supported on the object element for the root element.

  • 必须声明x:Class特性(这要求该 xaml 文件的生成操作必须是Page或其他一些编译该 xaml 的生成操作)。You must declare an x:Class attribute (which entails that the build action for this XAML file must be Page or some other build action that compiles the XAML).

  • ObservableCollection<T>位于命名空间和程序集中,最初不会映射到默认的 XML 命名空间。ObservableCollection<T> is in a namespace and assembly that are not initially mapped to the default XML namespace. 必须映射命名空间和程序集的前缀,然后在的对象元素标记ObservableCollection<T>中使用该前缀。You must map a prefix for the namespace and assembly, and then use that prefix on the object element tag for ObservableCollection<T>.

在应用程序中使用ObservableCollection<T> XAML 的功能更为直接的方法是声明自己的非泛型自定义集合类ObservableCollection<T>,该类派生自,并将其限制为特定类型。A more straightforward way to use ObservableCollection<T> capabilities from XAML in an application is to declare your own non-generic custom collection class that derives from ObservableCollection<T>, and constrains it to a specific type. 然后,映射包含此类的程序集,并将其作为 XAML 中的对象元素进行引用。Then map the assembly that contains this class, and reference it as an object element in your XAML.

构造函数

ObservableCollection<T>()

初始化 ObservableCollection<T> 类的新实例。Initializes a new instance of the ObservableCollection<T> class.

ObservableCollection<T>(IEnumerable<T>)

初始化 ObservableCollection<T> 类的新实例,该类包含从指定集合中复制的元素。Initializes a new instance of the ObservableCollection<T> class that contains elements copied from the specified collection.

ObservableCollection<T>(List<T>)

初始化 ObservableCollection<T> 类的新实例,该类包含从指定列表中复制的元素。Initializes a new instance of the ObservableCollection<T> class that contains elements copied from the specified list.

属性

Count

获取 Collection<T> 中实际包含的元素数。Gets the number of elements actually contained in the Collection<T>.

(继承自 Collection<T>)
Item[Int32]

获取或设置指定索引处的元素。Gets or sets the element at the specified index.

(继承自 Collection<T>)
Items

获取 IList<T> 周围的 Collection<T> 包装。Gets a IList<T> wrapper around the Collection<T>.

(继承自 Collection<T>)

方法

Add(T)

将对象添加到 Collection<T> 的结尾处。Adds an object to the end of the Collection<T>.

(继承自 Collection<T>)
BlockReentrancy()

不允许可重入的更改此集合的尝试。Disallows reentrant attempts to change this collection.

CheckReentrancy()

检查企图更改此集合的可重入尝试。Checks for reentrant attempts to change this collection.

Clear()

Collection<T> 中移除所有元素。Removes all elements from the Collection<T>.

(继承自 Collection<T>)
ClearItems()

从集合中移除所有项。Removes all items from the collection.

Contains(T)

确定某元素是否在 Collection<T> 中。Determines whether an element is in the Collection<T>.

(继承自 Collection<T>)
CopyTo(T[], Int32)

从目标数组的指定索引处开始将整个 Collection<T> 复制到兼容的一维 ArrayCopies the entire Collection<T> to a compatible one-dimensional Array, starting at the specified index of the target array.

(继承自 Collection<T>)
Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetEnumerator()

返回循环访问 Collection<T> 的枚举数。Returns an enumerator that iterates through the Collection<T>.

(继承自 Collection<T>)
GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
IndexOf(T)

搜索指定的对象,并返回整个 Collection<T> 中第一个匹配项的从零开始的索引。Searches for the specified object and returns the zero-based index of the first occurrence within the entire Collection<T>.

(继承自 Collection<T>)
Insert(Int32, T)

将元素插入 Collection<T> 的指定索引处。Inserts an element into the Collection<T> at the specified index.

(继承自 Collection<T>)
InsertItem(Int32, T)

将一项插入集合中指定索引处。Inserts an item into the collection at the specified index.

MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
Move(Int32, Int32)

将指定索引处的项移至集合中的新位置。Moves the item at the specified index to a new location in the collection.

MoveItem(Int32, Int32)

将指定索引处的项移至集合中的新位置。Moves the item at the specified index to a new location in the collection.

OnCollectionChanged(NotifyCollectionChangedEventArgs)

引发带有提供的参数的 CollectionChanged 事件。Raises the CollectionChanged event with the provided arguments.

OnPropertyChanged(PropertyChangedEventArgs)

引发带有提供的参数的 PropertyChanged 事件。Raises the PropertyChanged event with the provided arguments.

Remove(T)

Collection<T> 中移除特定对象的第一个匹配项。Removes the first occurrence of a specific object from the Collection<T>.

(继承自 Collection<T>)
RemoveAt(Int32)

移除 Collection<T> 的指定索引处的元素。Removes the element at the specified index of the Collection<T>.

(继承自 Collection<T>)
RemoveItem(Int32)

移除集合中指定索引处的项。Removes the item at the specified index of the collection.

SetItem(Int32, T)

替换指定索引处的元素。Replaces the element at the specified index.

ToString()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)

事件

CollectionChanged

在添加、移除、更改或移动项或者在刷新整个列表时发生。Occurs when an item is added, removed, changed, moved, or the entire list is refreshed.

PropertyChanged

在属性值更改时发生。Occurs when a property value changes.

显式界面实现

ICollection.CopyTo(Array, Int32)

从特定的 ICollection 索引开始,将 Array 的元素复制到一个 Array 中。Copies the elements of the ICollection to an Array, starting at a particular Array index.

(继承自 Collection<T>)
ICollection.IsSynchronized

获取一个值,该值指示是否同步对 ICollection 的访问(线程安全)。Gets a value indicating whether access to the ICollection is synchronized (thread safe).

(继承自 Collection<T>)
ICollection.SyncRoot

获取可用于同步对 ICollection 的访问的对象。Gets an object that can be used to synchronize access to the ICollection.

(继承自 Collection<T>)
ICollection<T>.IsReadOnly

获取一个值,该值指示 ICollection<T> 是否为只读。Gets a value indicating whether the ICollection<T> is read-only.

(继承自 Collection<T>)
IEnumerable.GetEnumerator()

返回循环访问集合的枚举数。Returns an enumerator that iterates through a collection.

(继承自 Collection<T>)
IList.Add(Object)

将某项添加到 IList 中。Adds an item to the IList.

(继承自 Collection<T>)
IList.Contains(Object)

确定 IList 是否包含特定值。Determines whether the IList contains a specific value.

(继承自 Collection<T>)
IList.IndexOf(Object)

确定 IList 中特定项的索引。Determines the index of a specific item in the IList.

(继承自 Collection<T>)
IList.Insert(Int32, Object)

将某项插入 IList 中指定的索引处。Inserts an item into the IList at the specified index.

(继承自 Collection<T>)
IList.IsFixedSize

获取一个值,该值指示 IList 是否具有固定大小。Gets a value indicating whether the IList has a fixed size.

(继承自 Collection<T>)
IList.IsReadOnly

获取一个值,该值指示 IList 是否为只读。Gets a value indicating whether the IList is read-only.

(继承自 Collection<T>)
IList.Item[Int32]

获取或设置指定索引处的元素。Gets or sets the element at the specified index.

(继承自 Collection<T>)
IList.Remove(Object)

IList 中移除特定对象的第一个匹配项。Removes the first occurrence of a specific object from the IList.

(继承自 Collection<T>)
INotifyPropertyChanged.PropertyChanged

在属性值更改时发生。Occurs when a property value changes.

扩展方法

CopyToDataTable<T>(IEnumerable<T>)

在给定其泛型参数 TDataTable 的输入 DataRow 对象的情况下,返回包含 IEnumerable<T> 对象副本的 DataRowReturns a DataTable that contains copies of the DataRow objects, given an input IEnumerable<T> object where the generic parameter T is DataRow.

CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption)

在给定其泛型参数 TDataRow 的输入 DataTable 对象的情况下,将 IEnumerable<T> 对象复制到指定的 DataRowCopies DataRow objects to the specified DataTable, given an input IEnumerable<T> object where the generic parameter T is DataRow.

CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption, FillErrorEventHandler)

在给定其泛型参数 TDataRow 的输入 DataTable 对象的情况下,将 IEnumerable<T> 对象复制到指定的 DataRowCopies DataRow objects to the specified DataTable, given an input IEnumerable<T> object where the generic parameter T is DataRow.

Cast<TResult>(IEnumerable)

IEnumerable 的元素强制转换为指定的类型。Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable)

根据指定类型筛选 IEnumerable 的元素。Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable)

启用查询的并行化。Enables parallelization of a query.

AsQueryable(IEnumerable)

IEnumerable 转换为 IQueryableConverts an IEnumerable to an IQueryable.

Ancestors<T>(IEnumerable<T>)

返回元素集合,其中包含源集合中每个节点的上级。Returns a collection of elements that contains the ancestors of every node in the source collection.

Ancestors<T>(IEnumerable<T>, XName)

返回经过筛选的元素集合,其中包含源集合中每个节点的上级。Returns a filtered collection of elements that contains the ancestors of every node in the source collection. 集合中仅包括具有匹配 XName 的元素。Only elements that have a matching XName are included in the collection.

DescendantNodes<T>(IEnumerable<T>)

返回源集合中每个文档和元素的子代节点的集合。Returns a collection of the descendant nodes of every document and element in the source collection.

Descendants<T>(IEnumerable<T>)

返回元素集合,其中包含源集合中每个元素和文档的子代元素。Returns a collection of elements that contains the descendant elements of every element and document in the source collection.

Descendants<T>(IEnumerable<T>, XName)

返回经过筛选的元素集合,其中包含源集合中每个元素和文档的子代元素。Returns a filtered collection of elements that contains the descendant elements of every element and document in the source collection. 集合中仅包括具有匹配 XName 的元素。Only elements that have a matching XName are included in the collection.

Elements<T>(IEnumerable<T>)

返回源集合中每个元素和文档的子元素的集合。Returns a collection of the child elements of every element and document in the source collection.

Elements<T>(IEnumerable<T>, XName)

返回源集合中经过筛选的每个元素和文档的子元素集合。Returns a filtered collection of the child elements of every element and document in the source collection. 集合中仅包括具有匹配 XName 的元素。Only elements that have a matching XName are included in the collection.

InDocumentOrder<T>(IEnumerable<T>)

返回节点集合(其中包含源集合中的所有节点),并按文档顺序排列。Returns a collection of nodes that contains all nodes in the source collection, sorted in document order.

Nodes<T>(IEnumerable<T>)

返回源集合中每个文档和元素的子节点集合。Returns a collection of the child nodes of every document and element in the source collection.

Remove<T>(IEnumerable<T>)

将源集合中的每个节点从其父节点中移除。Removes every node in the source collection from its parent node.

适用于

另请参阅