WPF Multiple CollectionView with different filters on same collection

前端 未结 2 1326
遥遥无期
遥遥无期 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: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.

提交回复
热议问题