Burt
Burt

Reputation: 7758

Filtering a collection inside a collection MVVM

I have a list of project dtos that contain a collection of tasks. On my ViewModel I have an ICollectionView for the projects so I can filter projects that are marked as done see below filter code.

    public void FilterDoneItems()
    {
        if (this.MarkDone)
        {
            ProjectsViewSource.Filter = new Predicate<object>(FilterDone);
        }
        else
        {
            ProjectsViewSource.Filter = null;
        }
    }

This works fine for projects but I also want to filter out the done tasks. As the ProjectDTO encompases the Tasks (List) I can't wrap the tasks in an ICollectionView to filter them in the ViewModel.

I am unsure how best to go about filtering on the tasks as well can anyone help please?

Upvotes: 2

Views: 810

Answers (1)

Quartermeister
Quartermeister

Reputation: 59139

Every collection has a default CollectionView maintained by WPF, and when you bind to the collection WPF will actually bind to that view. You can get a reference to that view by calling CollectionViewSource.GetDefaultView and set the filter on that:

CollectionViewSource.GetDefaultView(someList).Filter = somePredicate;

Upvotes: 1

Related Questions