Search code examples
c#exceptiondeferredicollectionview

CollectionView.DeferRefresh() throws exception


There are cases when you have many UI updates due a massive amount of INotifyChangedProperties events. In that case you might want to signal the changes to UI only once when all the properties are set like in a batch.

I found this great article that explains how to defer the refresh of the ViewCollection:

http://marlongrech.wordpress.com/2008/11/22/icollectionview-explained/

However I get an exception when the View is deferred and I try to add something to the collection. I don't understand why this shouldn't be allowed. That's the whole point of it in first place.

InvalidoperationException: Cannot change or check the contents or Current position of CollectionView while Refresh is being deferred.

Does anyone know how to solve this problem? Many Thanks,


Solution

  • Yes, don't modify the collection in question. I think that you are misunderstanding the purpose of a collection view. This is what MSDN says;

    You can think of a collection view as a layer on top of a binding source collection that allows you to navigate and display the collection based on sort, filter, and group queries, all without having to manipulate the underlying source collection itself.

    So in short, don't defer the refresh until you are done adding and removing, and otherwise manipulating your collection.