Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implenting ListCollectionView From ObservableCollection

I recently posted a question onto CodeReview (CodeReview Question) and following their advice I am looking from moving from an ObservableCollection to an ICollectionView to a ListCollectionView instead as apparently a ListCollectionView has better filtering performance.

What I do at the moment is this;

Contracts = await ReturnContracts();
ContractsICollectionView = CollectionViewSource.GetDefaultView(Contracts);
DataContext = this;

Where Contracts is the ObservableCollection and ContractsICollectionView is the ICollectionView. When I use a ListCollectionView instead I get this error;

Cannot convert from ICollectionView to ListCollectionView.

Here is the definition of Contracts and ContractsListCollectionView;

public ObservableCollection<ContractModel> Contracts;
public ListCollectionView ContractsListCollectionView { get; private set; }

My question is how can I implement ListCollectionView and take advantage of its improved filtering?

like image 558
CBreeze Avatar asked Sep 16 '25 15:09

CBreeze


1 Answers

Just declare ContractsListCollectionView like this:

public ICollectionView ContractsListCollectionView { get; private set; }

Alternatively, if you really need use ListCollectionView methods, and not just ICollectionView (note that ListCollectionView implements ICollectionView), then you need to make a cast:

ContractsICollectionView = (ListCollectionView) CollectionViewSource.GetDefaultView(Contracts);

Note that while for ObservableCollection CollectionViewSource.GetDefaultView indeed returns ListCollectionView - for other collection types it might not be the same and cast will fail. However since you are using it only with ObservableCollection - cast is fine.

like image 124
Evk Avatar answered Sep 18 '25 09:09

Evk