ObservableCollection 类

表示一个动态数据集合,在添加项、移除项或刷新整个列表时,此集合将提供通知。

命名空间: System.Collections.ObjectModel
程序集: System(在 System.dll 中)
用于 XAML 的 XMLNS:未映射到 xmlns。
[SerializableAttribute]
public class ObservableCollection<T> : Collection<T>, 
	INotifyCollectionChanged, INotifyPropertyChanged

请参见备注。

类型参数

T

集合中的元素类型。

ObservableCollection<T> 类型公开以下成员。

名称 说明
公共方法 ObservableCollection<T>() 初始化 ObservableCollection<T> 类的新实例。
公共方法 ObservableCollection<T>(IEnumerable<T>) 初始化 ObservableCollection<T> 类的新实例,该类包含从指定集合中复制的元素。
公共方法 ObservableCollection<T>(List<T>) 初始化 ObservableCollection<T> 类的新实例,该类包含从指定列表中复制的元素。
页首
名称 说明
公共属性 Count 获取 Collection<T> 中实际包含的元素数。 (继承自 Collection<T>。)
公共属性 Item 获取或设置指定索引处的元素。 (继承自 Collection<T>。)
受保护的属性 Items 获取 Collection<T> 周围的 IList<T> 包装。 (继承自 Collection<T>。)
页首
名称 说明
公共方法 Add 将对象添加到 Collection<T> 的结尾处。 (继承自 Collection<T>。)
受保护的方法 BlockReentrancy 不允许可重入的更改此集合的尝试。
受保护的方法 CheckReentrancy 检查可重入的更改此集合的尝试。
公共方法 Clear Collection<T> 中移除所有元素。 (继承自 Collection<T>。)
受保护的方法 ClearItems 从集合中移除所有项。 (重写 Collection<T>.ClearItems()。)
公共方法 Contains 确定某元素是否在 Collection<T> 中。 (继承自 Collection<T>。)
公共方法 CopyTo 从目标数组的指定索引处开始将整个 Collection<T> 复制到兼容的一维 Array (继承自 Collection<T>。)
公共方法 Equals(Object) 确定指定的 Object 是否等于当前的 Object (继承自 Object。)
受保护的方法 Finalize 允许对象在“垃圾回收”回收之前尝试释放资源并执行其他清理操作。 (继承自 Object。)
公共方法 GetEnumerator 返回循环访问 Collection<T> 的枚举数。 (继承自 Collection<T>。)
公共方法 GetHashCode 用作特定类型的哈希函数。 (继承自 Object。)
公共方法 GetType 获取当前实例的 Type (继承自 Object。)
公共方法 IndexOf 搜索指定的对象,并返回整个 Collection<T> 中第一个匹配项的从零开始的索引。 (继承自 Collection<T>。)
公共方法 Insert 将元素插入 Collection<T> 的指定索引处。 (继承自 Collection<T>。)
受保护的方法 InsertItem 将一项插入集合中指定索引处。 (重写 Collection<T>.InsertItem(Int32, T)。)
受保护的方法 MemberwiseClone 创建当前 Object 的浅表副本。 (继承自 Object。)
公共方法 Move 将指定索引处的项移至集合中的新位置。
受保护的方法 MoveItem 将指定索引处的项移至集合中的新位置。
受保护的方法 OnCollectionChanged 引发带有提供的参数的 CollectionChanged 事件。
受保护的方法 OnPropertyChanged 引发带有提供的参数的 PropertyChanged 事件。
公共方法 Remove Collection<T> 中移除特定对象的第一个匹配项。 (继承自 Collection<T>。)
公共方法 RemoveAt 移除 Collection<T> 的指定索引处的元素。 (继承自 Collection<T>。)
受保护的方法 RemoveItem 移除集合中指定索引处的项。 (重写 Collection<T>.RemoveItem(Int32)。)
受保护的方法 SetItem 替换指定索引处的元素。 (重写 Collection<T>.SetItem(Int32, T)。)
公共方法 ToString 返回表示当前对象的字符串。 (继承自 Object。)
页首
名称 说明
公共事件 CollectionChanged 在添加、移除、更改或移动项或者在刷新整个列表时发生。
受保护的事件 PropertyChanged 在更改属性值时发生。
页首
名称 说明
显式接口实现私有方法 ICollection.CopyTo 从特定的 Array 索引处开始,将 ICollection 的元素复制到一个 Array 中。 (继承自 Collection<T>。)
显式接口实现私有属性 ICollection<T>.IsReadOnly 获取一个值,该值指示 ICollection<T> 是否为只读。 (继承自 Collection<T>。)
显式接口实现私有属性 ICollection.IsSynchronized 获取一个值,该值指示是否同步对 ICollection 的访问(线程安全)。 (继承自 Collection<T>。)
显式接口实现私有属性 ICollection.SyncRoot 获取可用于同步对 ICollection 的访问的对象。 (继承自 Collection<T>。)
显式接口实现私有方法 IEnumerable.GetEnumerator 返回一个循环访问集合的枚举数。 (继承自 Collection<T>。)
显式接口实现私有方法 IList.Add 将某项添加到 IList 中。 (继承自 Collection<T>。)
显式接口实现私有方法 IList.Contains 确定 IList 是否包含特定值。 (继承自 Collection<T>。)
显式接口实现私有方法 IList.IndexOf 确定 IList 中特定项的索引。 (继承自 Collection<T>。)
显式接口实现私有方法 IList.Insert 将某项插入 IList 中指定的索引处。 (继承自 Collection<T>。)
显式接口实现私有属性 IList.IsFixedSize 获取一个值,该值指示 IList 是否具有固定大小。 (继承自 Collection<T>。)
显式接口实现私有属性 IList.IsReadOnly 获取一个值,该值指示 IList 是否为只读。 (继承自 Collection<T>。)
显式接口实现私有属性 IList.Item 获取或设置指定索引处的元素。 (继承自 Collection<T>。)
显式接口实现私有方法 IList.Remove IList 中移除特定对象的第一个匹配项。 (继承自 Collection<T>。)
显式接口实现私有事件 INotifyPropertyChanged.PropertyChanged 在更改属性值时发生。
页首

在许多情况下,所使用的数据是对象的集合。例如,数据绑定中的一个常见方案是使用 ItemsControl(如 ListBoxListViewTreeView)来显示记录的集合。

可以枚举实现 IEnumerable 接口的任何集合。但是,若要设置动态绑定,以使集合中的插入或移除操作可以自动更新 UI,则该集合必须实现 INotifyCollectionChanged 接口。此接口公开 CollectionChanged 事件,只要基础集合发生更改,都应该引发该事件。

WPF 提供 ObservableCollection<T> 类,它是实现 INotifyCollectionChanged 接口的数据集合的内置实现。

在实现自己的集合之前,请先考虑使用 ObservableCollection<T> 或现有的集合类之一,如 List<T>Collection<T>BindingList<T> 等。如果有高级方案并且希望实现自己的集合,请考虑使用 IList,它提供可以通过索引逐个访问的对象的非泛型集合。如果实现 IList,则将使用数据绑定引擎提供最佳性能。

注意注意

为了完全支持将绑定源对象中的数据值传送到绑定目标,在支持可绑定属性的集合中的每个对象都必须实现适当的属性更改通知机制,如 INotifyPropertyChanged 接口。

有关更多信息,请参见数据绑定概述中的“绑定到集合”。

关于 XAML 用法的说明

ObservableCollection<T> 可在 Windows Presentation Foundation (WPF) 3.0 和 3.5 版本中的一个 XAML 对象元素使用。但是,使用情况有众多限制。

  • ObservableCollection<T> 必须是根元素,因为指定泛型 ObservableCollection<T> 的约束类型所必须使用的 x:TypeArguments 特性只在根元素的对象元素上受支持。

  • 您必须声明一个 x:Class 特性(此属性要求该 XAML 文件的生成操作必须是 Page 或某种编译 XAML 的其他生成操作)。

  • ObservableCollection<T> 所在的命名空间和程序集最初未映射到默认的 XML 命名空间。您必须为该命名空间和程序集映射一个前缀,然后在 ObservableCollection<T> 的对象元素标记上使用该前缀。

在应用程序中使用 XAML 的 ObservableCollection<T> 功能的一种更直接的方法是声明自己的非泛型自定义集合类,该类派生自 ObservableCollection<T> 并将其约束为特定类型。然后映射包含此类的程序集,并将其作为 XAML 中的对象元素进行引用。

This example shows how to create and bind to a collection that derives from the ObservableCollection<T> class, which is a collection class that provides notifications when items get added or removed.

The following example shows the implementation of a NameList collection:

public class NameList : ObservableCollection<PersonName>
{
    public NameList() : base()
    {
        Add(new PersonName("Willa", "Cather"));
        Add(new PersonName("Isak", "Dinesen"));
        Add(new PersonName("Victor", "Hugo"));
        Add(new PersonName("Jules", "Verne"));
    }
  }

  public class PersonName
  {
      private string firstName;
      private string lastName;

      public PersonName(string first, string last)
      {
          this.firstName = first;
          this.lastName = last;
      }

      public string FirstName
      {
          get { return firstName; }
          set { firstName = value; }
      }

      public string LastName
      {
          get { return lastName; }
          set { lastName = value; }
      }
  }


You can make the collection available for binding the same way you would with other common language runtime (CLR) objects, as described in 如何:使数据可用于 XAML 中的绑定. For example, you can instantiate the collection in XAML and specify the collection as a resource, as shown here:


<Window
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:c="clr-namespace:SDKSample"
  x:Class="SDKSample.Window1"
  Width="400"
  Height="280"
  Title="MultiBinding Sample">
	
  <Window.Resources>
    <c:NameList x:Key="NameListData"/>


...


</Window.Resources>


You can then bind to the collection:

<ListBox Width="200"
         ItemsSource="{Binding Source={StaticResource NameListData}}"
         ItemTemplate="{StaticResource NameItemTemplate}"
         IsSynchronizedWithCurrentItem="True"/>


ObservableCollection<T> 类

原文地址:https://www.cnblogs.com/ITBread/p/2223215.html