0

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");
            }
        }
    }
  • do you use the CollectionView return by GetDefaultView(...) to fill the listbox? if yes the problem probably is that the comboBox also uses GetDefaultView(...) to get an CollectionView for the list your gave it, so they use the same CollectionView-Object – Ackdari Feb 18 '19 at 15:36
  • Hi, yes... It is bounded to the listbox as the CategoriesCollection. How can I fix this? –  Feb 19 '19 at 00:38

1 Answers1

1

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);
mm8
  • 163,881
  • 10
  • 57
  • 88
  • Is there any difference between ListCollectionView and ICollectionView? Should I replace this line CollectionViewSource.GetDefaultView(Categories); in my constructor with your line? –  Feb 19 '19 at 00:10
  • I'm using the ListCollectionView right now, but I have a problem. Let's say I add a new item to the observablecollection, since this collection is the one I'm using to manipulate, I also want the listcollectionview to update corresponding to the observablecollection, how can I do this? –  Feb 19 '19 at 08:22
  • @Bladeluster: The difference is that `ICollectionView` is an interface and `ListCollectionView` is a concrete implementation of that interface. And yes, you should replace the call to `CollectionViewSource.GetDefaultView(Categories)` in the constructor. Adding a new `Category` to the `ObservableCollection` will update the `ListCollectionView` as well. It's still just a view with a reference to the source collection. – mm8 Feb 19 '19 at 12:19
  • it seems like if I replace the object with another object of the same ID, just with different name, doesn't update the collectionView. How can I fix this? I'm mapping the new object over the old one. –  Feb 19 '19 at 12:33
  • @Bladeluster: How do you replace the object? – mm8 Feb 19 '19 at 12:34