.net中ObservableCollection的用法是什么?

.net中ObservableCollection的用法是什么?

ObservableCollection是一个集合,它允许集合外的代码知道对集合(添加,移动,移除)的更改何时发生。 它在WPF和Silverlight中被大量使用,但它的使用并不局限于此。 代码可以添加事件处理程序来查看集合何时发生更改,然后通过事件处理程序做出一些额外的处理。 这可能会改变用户界面或执行其他操作。

下面的代码并没有做任何事情,只是演示了如何在类中附加一个处理程序,然后使用事件参数以某种方式对这些更改作出反应。 WPF已经有了许多操作,比如刷新内置的UI,所以在使用ObservableCollections时可以免费获取它们

class Handler { private ObservableCollection<string> collection; public Handler() { collection = new ObservableCollection<string>(); collection.CollectionChanged += HandleChange; } private void HandleChange(object sender, NotifyCollectionChangedEventArgs e) { foreach (var x in e.NewItems) { // do something } foreach (var y in e.OldItems) { //do something } if (e.Action == NotifyCollectionChangedAction.Move) { //do something } } } 

ObservableCollection基本上像普通的集合一样工作,只是它实现了接口:

  • INotifyCollectionChanged
  • INotifyPropertyChanged

因此,当您想知道集合何时更改时,它非常有用。 会触发一个事件,告诉用户哪些条目已被添加/删除或移动。

更重要的是,它们在表单上使用数据绑定时非常有用。

从Pro C#5.0和.NET 4.5框架

ObservableCollection<T>类是非常有用的,因为它能够在外部对象以某种方式发生更改时通知外部对象(正如您可能会猜到的那样,使用ReadOnlyObservableCollection<T>非常相似,但本质上是只读的) 。 在很多方面,使用ObservableCollection<T>与使用List<T> ObservableCollection<T>是一样的,因为这两个类实现了相同的核心接口。 什么使得ObservableCollection<T>类是唯一的,就是这个类支持一个名为CollectionChanged的事件。 无论何时插入新项目,当前项目被移除(或重新定位),或者如果整个集合被修改,该事件都将触发。 像任何事件一样,CollectionChanged是根据委托来定义的,在这个例子中是NotifyCollectionChangedEventHandler 。 这个委托可以调用任何将对象作为第一个参数的方法,并且可以调用一个NotifyCollectionChangedEventArgs作为第二个参数。 考虑下面的Main()方法,该方法填充一个包含Person对象的可观察集合,并连接CollectionChanged事件:

 class Program { static void Main(string[] args) { // Make a collection to observe and add a few Person objects. ObservableCollection<Person> people = new ObservableCollection<Person>() { new Person{ FirstName = "Peter", LastName = "Murphy", Age = 52 }, new Person{ FirstName = "Kevin", LastName = "Key", Age = 48 }, }; // Wire up the CollectionChanged event. people.CollectionChanged += people_CollectionChanged; // Now add a new item. people.Add(new Person("Fred", "Smith", 32)); // Remove an item. people.RemoveAt(0); Console.ReadLine(); } static void people_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { // What was the action that caused the event? Console.WriteLine("Action for this event: {0}", e.Action); // They removed something. if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove) { Console.WriteLine("Here are the OLD items:"); foreach (Person p in e.OldItems) { Console.WriteLine(p.ToString()); } Console.WriteLine(); } // They added something. if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add) { // Now show the NEW items that were inserted. Console.WriteLine("Here are the NEW items:"); foreach (Person p in e.NewItems) { Console.WriteLine(p.ToString()); } } } } 

传入的NotifyCollectionChangedEventArgs参数定义了两个重要的属性OldItemsNewItems ,它们将为您提供事件触发之前当前在集合中的项目列表以及参与更改的新项目。 但是,只有在正确的情况下才能查看这些列表。 回想一下,当项目被添加,删除,重定位或重置时,CollectionChanged事件可能会触发。 要发现哪些操作触发了事件,可以使用NotifyCollectionChangedEventArgs的Action属性。 可以针对NotifyCollectionChangedAction枚举的以下任何成员testingAction属性:

 public enum NotifyCollectionChangedAction { Add = 0, Remove = 1, Replace = 2, Move = 3, Reset = 4, } 

System.Collections.ObjectModel的成员

最大的用处之一是你可以将UI组件绑定到一个,如果集合的内容发生变化,他们会做出适当的响应。 例如,如果将一个ListView的ItemsSource绑定到一个ListView的内容将自动更新,如果您修改该集合。

编辑:这里是一些示例代码从MSDN: http : //msdn.microsoft.com/en-us/library/ms748365.aspx

在C#中,将ListBox连接到集合可能非常简单

 listBox.ItemsSource = NameListData; 

尽pipe如果你没有把列表挂钩为静态资源并定义了NameItemTemplate,你可能想重写PersonName的ToString()。 例如:

 public override ToString() { return string.Format("{0} {1}", this.FirstName, this.LastName); } 

对于那些想要一个简单的答案,我会把我的手,并进行尝试:

普通collections – 没有通知

我时不时地去纽约,我的朋友要我买东西。 所以我和我一起购物清单。 名单上有很多东西在那里:

  1. 维多利亚的秘密
  2. 克莱夫基督教皇帝陛下($ 215,000)
  3. 法拉利($ 750,000)

那么我不会购买这些东西,所以我把它们从列表中删除,然后添加:

  1. 12打Titleist高尔夫球。

事情是,我的朋友不知道我从名单上删除了什么,以及我添加了什么。 朋友没有得到通知。

ObservableCollection – 发生更改时的通知

所以我通常没有货物回家,她从来不高兴。

现在,Buuuut在手机上有一个应用程序:每当我从列表中删除某些东西时:她会在手机上收到通知(即短信或电子邮件等)!

可观察的收集工作方式是一样的。 如果您添加或删除某些内容,则会通知某人。 当他们得到通知时,那么他们打电话给你,你会得到满意的。 当然,后果可以通过事件处理程序进行定制。

这一切总结起来!

它是一个用来通知UI集合更改的集合,它支持自动通知。

主要用于WPF,

在哪里说假设你有一个列表框和添加button,当你点击他button时,他button一个types的对象假设人将被添加到obseravablecollection,并将此集合绑定到ListSource的ItemSource,所以只要你添加集合中的新项目,Listbox将自行更新并在其中添加一个项目。

 class FooObservableCollection : ObservableCollection<Foo> { protected override void InsertItem(int index, Foo item) { base.Add(index, Foo); if (this.CollectionChanged != null) this.CollectionChanged(this, new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Add, item, index); } } var collection = new FooObservableCollection(); collection.CollectionChanged += CollectionChanged; collection.Add(new Foo()); void CollectionChanged (object sender, NotifyCollectionChangedEventArgs e) { Foo newItem = e.NewItems.OfType<Foo>().First(); }