Fire an event when Collection Changed (add or remove)
I have a class which contains a list :
public class开发者_运维知识库 a
{
private List<MyType> _Children;
public Children
{
get { return(_Children); }
set { _Children = value ; }
}
}
I want to create an event and fire it whenever my list (_Children here) is changed for example an item is added to it or removed from it or it's cleared.
thanks
Change your list to an ObservableCollection<T>
. It implements INotifyCollectionChanged, so you can subscribe to change events on it.
Another option is to use BindingList<T>
, if you need full list semantics.
See ObservableCollection
Or if you want to control the Add and remove methods and raise event, check Collection<T>
out.
http://msdn.microsoft.com/en-us/library/ms132397.aspx
精彩评论