0
votes

I have a ViewModel with some ObservableCollection _questions, which is loaded from DB when VM instance created. Also this list is used to collect data to save back to DB.

This Vm is used for a View1 and it displays the list in ListView with filtering by a property using CollectionViewSource.GetDefaultView(_questions).Filter = ...

Now I need to create View2 which will display same list but without filtering. I can't bind it to the same ObservableCollection _questions because it has filter defined on CollectionViewSource, but I need to use it to keep SaveToDb code same.

Is it possible to have different filtering on the same data source for two different ListViews?

1

1 Answers

1
votes

I have never enjoyed using CollectionViewSource. I would instead filter using a new property in my ViewModel that filters using Linq:

public IEnumerable<MyType> FilteredItems()
{
    get
    {
        return MyCollection.Where(x => x.MyProperty == SomeValue).ToArray;
    }
}

I would then bind ItemsSource to this property and use INotifyPropertyChanged event to notify UI of changes to the collection.

Its hard to tell if this fits your scenario well as there is not much information provided on what you need to achieve.