views:

43

answers:

1

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?

+1  A: 

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;
Quartermeister
Would I have to bind to the CollectionViewSource or could I still bind to the actual collection and have the filtering work?
Burt
@Burt: You still bind to the actual collection. From the CollectionViewSource.GetDefaultView doc: "If you bind directly to a collection, WPF actually binds to the default view for that collection."
Quartermeister