简体   繁体   中英

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?

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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