Reputation: 5896
Suppose you have a ViewModel with an ObservableCollection (OC) that is bound to some control inside the View.
Is there a way to temporarily disable the binding between the OC and the Control without removing the binding ? I want to have the ability to modify my ObservableCollection without having the View being informed of it.
The reason for that is simple: I am doing a lot of Add() and Insert() operations on the OC. Most of the time everything is OK but sometimes I am calling Add() so frequently that the updates in the View are looking unpleasant. For these periods of time I want to 'switch off' the binding beforehand and 'turn it on' afterwards.
Has anybody been in a similar situation / does anybody has a tip ?
Upvotes: 1
Views: 216
Reputation: 955
When I have a lot of items in the collection, I prefer to use a List<> as the source of a CollectionViewSource. Then the view binds to the CollectionViewSource.View. CollectionViewSource has methods DeferRefresh() and Refresh() that let you do all the background work of changing the source list without view notifications. The performance of CollectionViewSource is way better than an ObservableCollection, plus it supports filtering. Filtering might be what you need if you are constantly adding and removing items.
Upvotes: 1
Reputation: 62544
Just wrap up ObservableCollection
(even inheriting from it) and then override mthod OnCollectionChanged
by adding custom logic which will postpone event triggering considering your requirements, I believe this is a pretty standard way to implement own defferable observable collection.
Upvotes: 4