Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fire an event when Collection Changed (add or remove)

Tags:

c#

events

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

like image 220
Asha Avatar asked Oct 22 '09 14:10

Asha


2 Answers

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.

like image 173
Reed Copsey Avatar answered Nov 04 '22 20:11

Reed Copsey


See ObservableCollection

like image 28
Henrik Avatar answered Nov 04 '22 22:11

Henrik