CollectionView.DeferRefresh() throws exception
Asked Answered
F

2

11

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,

Fauve answered 17/5, 2011 at 14:14 Comment(0)
M
2

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.

Mat answered 17/5, 2011 at 14:55 Comment(2)
You haven't fully answered the question as DRAirey1 points out.Jilli
@Joey: DRAirey1 is speculating about how the feature "should" work as opposed to how it "actually" works. In the meantime, I think the little green checkmark indicates that the OP is satisfied with the answer.Mat
T
19

I think you are misunderstanding or misrepresenting the MSDN help on the subject. They are saying that you don't have to modify the underlying collection to sort or filter. They are not saying that you can't modify the underlying collection. The OP has a very valid point. We have a large collection that has been sorted and filter for the user and it is displayed in a list box. When the user selects a range of those records and wants to delete them, we are forced into a situation where the ListCollectionView refreshs view for every item that was deleted.

I think the OP's question is very valid. The performance is horrendous because we have multiple filters on a huge set of data. The DeferRefresh has a real purpose on a class like the ListCollectionView, but in inexplicably diabled for inserts and deletes, when you need it the most.

Tchad answered 16/3, 2012 at 17:3 Comment(2)
You are right, the docs don't explicitly say that you "can't", but the resulting exception does. Meanwhile, if ListCollectionView isn't doing what you want, make your own.Mat
DeferRefreshed is only aimed at sorting, grouping and filtering as per the classes Refresh method. It's only purpose is to defer the automatic refresh that happens when you add a new sort, filter or group. But it's true that it might be better by ignoring changes to the source for a while.Stansbury
M
2

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.

Mat answered 17/5, 2011 at 14:55 Comment(2)
You haven't fully answered the question as DRAirey1 points out.Jilli
@Joey: DRAirey1 is speculating about how the feature "should" work as opposed to how it "actually" works. In the meantime, I think the little green checkmark indicates that the OP is satisfied with the answer.Mat

© 2022 - 2024 — McMap. All rights reserved.