简体   繁体   English

在集合MVVM中过滤集合

[英]Filtering a collection inside a collection MVVM

I have a list of project dtos that contain a collection of tasks. 我有一个包含任务集合的项目dto列表。 On my ViewModel I have an ICollectionView for the projects so I can filter projects that are marked as done see below filter code. 在我的ViewModel上,我有一个用于项目的ICollectionView,因此我可以过滤标记为已完成的项目,请参见下面的过滤器代码。

    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. 当ProjectDTO包含任务(列表)时,我无法将任务包装在ICollectionView中以在ViewModel中对其进行过滤。

I am unsure how best to go about filtering on the tasks as well can anyone help please? 我不确定如何最好地过滤任务,还有谁能帮忙吗?

Every collection has a default CollectionView maintained by WPF, and when you bind to the collection WPF will actually bind to that view. 每个集合都有WPF维护的默认CollectionView,并且当您绑定到集合时,WPF实际上将绑定到该视图。 You can get a reference to that view by calling CollectionViewSource.GetDefaultView and set the filter on that: 您可以通过调用CollectionViewSource.GetDefaultView并在该视图上设置过滤器来获取对该视图的引用:

CollectionViewSource.GetDefaultView(someList).Filter = somePredicate;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM