1

ImageAlbums is an ICollectionView type and GlobalCollection.MyImageAlubms is an ObservableCollection<T> type.

ImageAlbums = CollectionViewSource.GetDefaultView(GlobalCollection.MyImageAlubms);
ImageAlbums.Filter = new Predicate<object>(this.FilterImageAlbumList);

In a view I'm using ImageAlbums for showing a filtered image list. I have filtered the list using FilterImageAlbumList method. The problem is I have used the GlobalCollection.MyImageAlubms in another place. In that view I have used the GlobalCollection.MyImageAlubms directly as source but in there the list are being showed as filtered also. I am also providing the filter method here, following code represents the filter method

private bool FilterImageAlbumList(object item)
{
    AlbumModel albumMoel = (AlbumModel)item;
    if(LOGIC_OF_FILTERING)
    {
        return false;
    }
    return true;
}

Is there any way to filter only ImageAlbums without affecting the GlobalCollection. FYI - I won't deep copy the Global Collection.

Yael
  • 1,566
  • 3
  • 18
  • 25
lukai
  • 536
  • 1
  • 5
  • 20
  • Since item is an object you can test the type. Each class has a different enumeration for type. – jdweng Nov 29 '16 at 10:27
  • You can create a new view with `new CollectionViewSource { Source = GlobalCollection.MyImageAlubms}.View;` however I've found this buggy in the past (http://stackoverflow.com/questions/37166747/object-reference-not-set-to-an-instance-of-an-object-in-presentationframework) and ended up just creating copies... – Joe Nov 29 '16 at 10:29
  • @Joe, if you found this buggy why you are suggesting this ? – lukai Nov 29 '16 at 10:45
  • 1
    @lukai that's why I put it as a comment, not an answer. Just giving options. Someone else might suggest it as an answer without knowing it's faults. – Joe Nov 29 '16 at 10:59
  • @Joem thanks. Got the solution already though. – lukai Nov 29 '16 at 11:03

1 Answers1

3

Your problem is caused by these two facts:

  1. Each collection instance has only one default (instance of the) view, thus CollectionViewSource.GetDefaultView always returns the same instance for the same argument
  2. WPF binding mechanism does not bind directly to a collection, but to its default collection view

So if you set a filter on the default view, its effects are visible wherever you bind to the collection.

If you want a separate instance of an ICollectionView your best bet is to instantiate it manually. For ObservableCollection<T> a good choice is ListCollectionView. So this should resolve your problems:

ImageAlbums = new ListCollectionView(GlobalCollection.MyImageAlubms);
Grx70
  • 10,041
  • 1
  • 40
  • 55