简体   繁体   English

ObservableCollection(T).CollectionChanged事件

[英]ObservableCollection(T).CollectionChanged Event

SelectedObjects.CollectionChanged += (s, e) =>
{
    Console.WriteLine(e.NewItems.Count);//prints out 1 if a new item is added
};

one thing i am not sure of is why e.OldItems and e.NewItems are collections? 有一件事我不确定是为什么e.OldItems和e.NewItems是收藏品?

if i add multiple items per second the event gets fired foreach item and the output is always 1 meaning that it gets fired multiple times, but is there a way to delay the event firing, for example after 5 seconds of the change so that i could get every change in those 5 seconds in the e.OldItems or the e.NewItems? 如果我每秒添加多个项目,事件会被触发foreach项目,输出总是1,这意味着它被多次触发,但有没有办法延迟事件触发,例如在更改5秒后我可以在e.OldItems或e.NewItems中获得5秒内的每一个变化?

No. The point of an observable collection is to raise the event when it is modified. 不可以。可观察集合的要点是在修改事件时提升事件。 Whilst in your use-case it is desirable to be notified after a set amount of time, it is not every use-case and would over-complicate the class. 虽然在您的用例中,希望在一段时间后得到通知,但并不是每个用例都会使课程过于复杂。

What you could do is wrap the observable collection in to a custom class that you create (eg DelayedObservableCollection<T> , and that - in conjunction with a timer -, raises its' own event when items are added/modified, and then no items are added/modified after a specified time, or more than 'x' items are added/modified. 您可以做的是将可观察集合包装到您创建的自定义类中(例如, DelayedObservableCollection<T> ,并且 - 与计时器一起 - 在添加/修改项目时引发其自己的事件,然后没有项目在指定时间后添加/修改,或者添加/修改超过“x”项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM