tags:

views:

246

answers:

3

Hi all, 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 that event whenever my list (_Children here) is changed for example an item is added to it or removed from it or it's cleared .

thanks

+5  A: 

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.

Reed Copsey
One thing to remember about ObservableCollection is it will throw an exception if you try to add an element to it from thread other than the one you created it on (whether or not you lock on it).
280Z28
Very good point, 280Z28.
Reed Copsey
+2  A: 

See ObservableCollection

Henrik
A: 

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

Mehdi Golchin