user10954933
user10954933

Reputation:

ICollectionView adds filter to 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.

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");
            }
        }
    }

Upvotes: 0

Views: 292

Answers (1)

mm8
mm8

Reputation: 169270

You are binding to the same view: Should I bind to ICollectionView or ObservableCollection

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);

Upvotes: 1

Related Questions