Need a collection class with events
I would need a collect开发者_运维知识库ion class in C# that fires an event when I add or remove an item. Is there such a collection class?
ObservableCollection<T>
should work for most cases. It implements INotifyCollectionChanged
, which is an interface which :
Notifies listeners of dynamic changes, such as when items get added and removed or the whole list is refreshed.
You could take a look at the ObservableCollection
class, it has events for the CollectionChanged
which handles when an item is added, removed, changed, moved, or the entire list is refreshed..
System.Collections.ObjectModel.ObservableCollection implemented INotifyCollectionChanged
, which is probably what you want. Note that prior to .NET 4, this class is in a WPF assembly.
You can always implement INotifyCollectionChanged
yourself, as well.
Besides ObervableCollection
, there is a second possible candidate, the BindingList<T>
.
精彩评论