Mutiple Refreshable CollectionViewSources of single Observable Collection

1

Is there any way that I can have multiple views on a single Observable Collection, with different filters, that I can Refresh?

I have an Observable Collection called Proficiencies. I have three list boxes, each should display a subset of the Proficiencies items, filtered on a value within the Proficiencies items. i.e. One list displays items of category A, one list displays items of category B & one list displays items of category C.

I am trying to filter the collection using CollectionViewSources (called the SkillsView, ToolsView, and LanguagesView, one for each list box, each with it's own filter. They are properties in my ViewModel class that the lists boxes Bind to. they are declared in the form of:

    protected ICollectionView theSkillsView;

    public ICollectionView SkillsView
    {
        get { return theSkillsView; }
        protected set
        {
            theSkillsView = value;
            OnPropertyChanged("SkillsView");
        }
    }

I have two ways of initialising them either (1):

        theSkillsView = new CollectionViewSource { Source = Proficiencies }.View;
        theToolsView = new CollectionViewSource { Source = Proficiencies }.View;
        theLanguagesView = new CollectionViewSource { Source = Proficiencies }.View;

Or an alternative Ive found (2):

        theSkillsView = CollectionViewSource.GetDefaultView(Proficiencies);
        theToolsView =  CollectionViewSource.GetDefaultView(Proficiencies);
        theLanguagesView = CollectionViewSource.GetDefaultView(Proficiencies);

I then Apply the Filtering:

        theLanguagesView.Filter = p => ((ProficiencyData) p).Category == ProficiencyCategoryType.Language;
        theSkillsView.Filter = p => ((ProficiencyData) p).Category == ProficiencyCategoryType.Skill;
        theToolsView.Filter = p => ((ProficiencyData) p).Category == ProficiencyCategoryType.Tool;

The problem is: if I use Option (1) all the views have the same filter (which ever is applied last) If I use option (2) when I call Refresh() I get the following error:

System.NullReferenceException
  HResult=0x80004003
  Message=Object reference not set to an instance of an object.
  Source=PresentationFramework
  StackTrace:
   at System.Windows.Data.ListCollectionView.PrepareLocalArray()

I could create three seperate Collections, but that a) means I don't have all the items in a single collection and b) seems to be not in the spirit of things when CollectionViewSources are available precisely for Sorting, grouping and Filtering.

Many thanks in advance for any suggestions.

c#
wpf

1 Answer

1

You should create three separate views of the AllItemsInDataBase source collection:

theSkillsView = new ListCollectionView(AllItemsInDataBase);
theToolsView  = new ListCollectionView(AllItemsInDataBase);
theLanguagesView = new ListCollectionView(AllItemsInDataBase);

You can then filter the views independently of each other.

answered on Stack Overflow Aug 10, 2020 by mm8

User contributions licensed under CC BY-SA 3.0