开发者

How can I tell when a collection has been edited?

开发者 https://www.devze.com 2023-01-08 23:30 出处:网络
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?

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?


Can you use the ObservableCollection?

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


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);
    }
}


Try ObservableCollection

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

0

精彩评论

暂无评论...
验证码 换一张
取 消