Malfist
Malfist

Reputation: 31795

How can I tell when a collection has been edited?

I have a public property called Items, It's a List. I want to tell when it's been altered. How can I do this?

For example, if Items.Add is called, I want to be able to then call UpdateInnerList.

How can I do this?

Upvotes: 0

Views: 84

Answers (3)

Shaun Bowe
Shaun Bowe

Reputation: 10008

Try ObservableCollection

It supports a CollectionChanged event which should be what you need.

Upvotes: 2

Wil P
Wil P

Reputation: 3371

Can you use the ObservableCollection?

http://msdn.microsoft.com/en-us/library/ms668604.aspx

Upvotes: 5

spender
spender

Reputation: 120480

How about creating a List subclass and overriding the Add method?

void Main()
{
    var x=new MySpecialList<string>();
    x.Add("hello");
}

class MySpecialList<T>:List<T>
{
    public new void Add(T item)
    {
        //special action here
        Console.WriteLine("added "+item);
        base.Add(item);
    }
}

Upvotes: 2

Related Questions