WPF Multiple CollectionView with different filters on same collection

前端 未结 2 1318
遥遥无期
遥遥无期 2020-12-07 17:54

I\'m using a an ObservableCollection with two ICollectionView for different filters.

One is for filtering messages by some type, and one is

相关标签:
2条回答
  • 2020-12-07 18:03

    This answer helped me with this exact problem. The static CollectionViewSource.GetDefaultView(coll) method will always return the same reference for a given collection, so basing multiple collection views on the same reference will be counterproductive. By instantiating the view as follows:

    ICollectionView filteredView = new CollectionViewSource { Source=messageList }.View;
    

    The view can now be filtered/sorted/grouped independently of any others. Then you can apply your filtering.

    I know it's been a couple months and you have probably solved your problem by now, but I ran across this question when I had the same problem so I figured I would add an answer.

    0 讨论(0)
  • 2020-12-07 18:04

    For anyone struggling with the problem that the filteredView does not observe the sourceCollection (messageList in this example):

    I came around with this solution:

    ICollectionView filteredView = new CollectionViewSource { Source=messageList }.View;
    messageList.CollectionChanged += delegate { filteredView.Refresh(); };
    

    So it will refresh our filteredView evertytime the CollectionChanged event of the source get's fired. Of course you can implement it like this too:

    messageList.CollectionChanged += messageList_CollectionChanged;
    
    private void messageList_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        filteredView.Refresh(); 
    }
    

    Consider using PropertyChanged-Events when filtering on a specific Property is wanted.

    0 讨论(0)
提交回复
热议问题