Sam King
Sam King

Reputation: 85

Collectionviewsource filter methods

I Follow MVVM model for my application, I have a textbox which acts as an input to filter the collection. I understood observablecollection filter using lambda expression but i couldn't understand collectionviewsource methods. how can i implement collectionviewsource methods using this.

Here is my viewmodel class:

private ObservableCollection<SPFetchCREntity> _CRmappings2 = new ObservableCollection<SPFetchCREntity>();
public ObservableCollection<SPFetchCREntity> CRmappings2
{
    get { return _CRmappings2; }
    set
    {
        _CRmappings2 = value;
        RaisePropertyChanged("CRmappings2");
    }
}

public ICollectionView AllCRSP


{ get; private set;}



public void UpdatePopList()
{
   CRmappings2 = new ObservableCollection<SPFetchCREntity>(crentities.Where(p => p.MU_Identifier == selectmu.ToString()).ToList()); 
}

Upvotes: 0

Views: 452

Answers (1)

mm8
mm8

Reputation: 169200

Bind to the ICollectionView and filter this one:

public void UpdatePopList()
{
    CRmappings2 = new ObservableCollection<SPFetchCREntity>(crentities.ToList());
    AllCRSP = CollectionViewSource.GetDefaultView(CRmappings2);
    AllCRSP.Filter = obj =>
    {
        SPFetchCREntity entity = obj as SPFetchCREntity;
        return entity != null && entity.MU_Identifier == selectmu.ToString();
    };
}

private string _selectmu;
public string Selectmu
{
    get { return _selectmu; }
    set { _selectmu = value; AllCRSP.Refresh(); } //<-- refresh the ICollectionView whenever the selectmu property gets set or when you want to refresh the filter
}

Upvotes: 1

Related Questions