CBreeze
CBreeze

Reputation: 2965

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?

Upvotes: 4

Views: 2274

Answers (1)

Evk
Evk

Reputation: 101453

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.

Upvotes: 2

Related Questions