I have in my VM ObservableCollection<MyClass> . Everything is great, when the collection is replenished, the view immediately displays the changes. But it took me in a separate place to display not the entire collection, but with a selection (such as where TheMyClass.Prop == someValue ). Well, by itself, this branch from the main collection should also respond to the arrival of new items. How to make it more literate? It just came to my mind several ways and not one like it.

For example, subscribe to change the main collection, create a separate collection in which to add only those elements that meet the condition.

Another option is to create a simple property with a selection through Where() , and update this property every time the main collection changes.

And what options do you have?

    1 answer 1

    You can use CollectionViewSource with a filter:

     <Window.Resources> <CollectionViewSource x:Key="Filtered" Source="{Binding Items}" Filter="CollectionViewSource_Filter" /> </Window.Resources> ... <ListBox ItemsSource="{Binding Source={StaticResource Filtered}}"></ListBox> 

    Items are ObservableCollection<TheMyClass> from VM, CollectionViewSource_Filter is an event handler with filtering logic:

     private void CollectionViewSource_Filter(object sender, FilterEventArgs e) { var item = (TheMyClass)e.Item; // Если выставить в false, элемент пропадет из фильтрованной коллекции. // По умолчанию true. if (item.Prop != someValue) e.Accepted = false; }