ICollectionView 向 ObservableCollection 添加过滤器 [英] ICollectionView adds filter to ObservableCollection

查看:49
本文介绍了ICollectionView 向 ObservableCollection 添加过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我向 ICollectionView 添加过滤器时,我的 WPF 应用程序出现问题,其中搜索过滤器应用于 observablecollection.我有两个视图,其中两个单独的视图模型.在这种情况下,一个视图允许您搜索和操作一个集合,第二个视图有一个组合框,允许用户从集合中选择一个项目.

I'm having problem with with my WPF application, where the search filter is applied to the observablecollection, when I add a filter to the ICollectionView. I got two views which two separate viewmodels. In this case, one view allows you to search on a collection and manipulate it, and the second view has a combobox which allows the user to choose an item from the collection.

首先,我将项目检索到我的 observablecollection 中,正如您在下面的代码中看到的那样.然后我正在设置 CollectionViewSource.现在,我正在向 CollectionView 添加过滤器,这是我实现的搜索.我的问题是我认为过滤器仅适用于我在列表框中使用的 ICollectionView 集合,但它表明它也适用于 ObservableCollection.列表框使用 CollectionView,组合框使用类别的 ObservableCollection.但我不希望将过滤器应用于使用 observablecollection 的组合框集合,因为我想一直显示所有可用项目.

At first, I'm retrieving the items to my observablecollection as you can see in the code under. Then I'm setting the CollectionViewSource. As now, I'm adding filter to the CollectionView, which is a search I've implemented. My problem is that I thought that the filter would only apply to the ICollectionView collection, which I'm using in the listbox, but it shows out that it also applies to the ObservableCollection. The listbox is using the CollectionView and the combobox is using the ObservableCollection of the categories. But I don't want the filter to be applied to the combobox collection, which uses the observablecolelction, as I want to show all the available items all the time.

我该如何解决这个问题?

How can I fix this?

    public ViewModel () 
    {
         CollectionViewSource.GetDefaultView(Categories); 
    }


    public ObservableCollection<Category> Categories
    {
        get
        {
            return this._categories;
        }
        set
        {
            if (this._categories!= value)
            {
                this._categories= value;
                this.OnPropertyChanged("Categories");
            }
        }
    }


    private ICollectionView _categoriesCollection;  
    public ICollectionView CategoriesCollection
    {
        get
        {
            return this._categoriesCollection;
        }
        set
        {
            if (this._categoriesCollection!= value)
            {
                this._categoriesCollection= value;

                this.OnPropertyChanged("CategoriesCollection");
            }
        }
    }

推荐答案

您绑定到同一个视图:我应该绑定到 ICollectionView 还是 ObservableCollection

不是将 CategoriesCollection 属性设置为 CollectionViewSource.GetDefaultView(_categories) 的返回值,您可以创建一个新视图来修复"此问题:

Instead of setting your CategoriesCollection property to the return value of CollectionViewSource.GetDefaultView(_categories), you could create a new view to "fix" this:

CategoriesCollection = new ListCollectionView(_categories);

这篇关于ICollectionView 向 ObservableCollection 添加过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆