Giacomo Tagliabue
Giacomo Tagliabue

Reputation: 1759

Filtering two ICollectionView from a single ObservableCollection

I have a problem using two ICollectionView instances that have the same ObservableCollection as source.

When I filter an ICollectionView, it seems that also the other ICollectionView is filtered with the same filter. I instantiate the ICollectionView with the method CollectionViewSource.GetDefaultView.

Is this the expected behaviour? I don't think so, but maybe I'm missing something.

This is the constructor of the ViewModel:

ListaVoci = CollectionViewSource.GetDefaultView(RootVM.CollectionVociCE);

where ListaVoci is an ICollectionView and RootVM.CollectionVociCE is an ObservableCollection. I have two different user controls that have two different instances of this ViewModel.

This is the constructor of the user control:

datacontext.ListaVoci.Filter = FiltraListaVoci;

where FiltraListaVoci is

public bool FiltraListaVoci(object filter)
{
    // I make some filtering
}

Upvotes: 2

Views: 1117

Answers (1)

Giacomo Tagliabue
Giacomo Tagliabue

Reputation: 1759

Ok, I solved the problem :) with CollectionViewSource.GetDefaultView() I get the same view instance for both the two ICollectionViews, so that they reference the same object. The right way to instantiate the ICollectionView in this case is this:

CollectionViewSource cvs = new CollectionViewSource();
cvs.Source = RootVM.CollectionVociSP;
ListaVoci = cvs.View;

So I create a Collectionviewsource object whenever the constructor is called. I hope that this doesn't lead to some strange side effects :) Thank you anyway!

Upvotes: 2

Related Questions