views:

168

answers:

1

Hi everyone,

I have several CollectionViewSource instances all used on the same ObservableCollection. I also have several controls that need to show filtered versions of the collection (hence the CollectionViewSources). The problem I'm having is that CollectionViewSource forces them to all have the same item selected. Is there some way to turn this off?

Thanks, Jason Lewis

A: 

If you use the method (im not sure about this but CollectionViewSource in xaml probably does)

CollectionViewSource.GetDefaultView(this.ItemsSource);

multiple times it will only return the same ICollectionView (if we are talking about the same collection bound multiple times), this means if you apply a filter to one it apllies to all, also as the collection view tracks the current item the current item will be syncronised between the different views.

You can work around this by creating a CollectionView for each filter/selection you want to have by using

new CollectionView(this.ItemsSource as IList);

there are a few types that implement ICollectionView, CollectionView and ListCollectionView do. in the above code i did not get the default view i created a new one, so its filering/sorting is unique.

When you use items source wpf creates a collection view to wrap the collection, this collection view is the one that is returned in the GetDefaultView call, useful for every day situations but not the edge cases.

here is a blog post from bea that explains it a bit better

Aran Mulholland
It's actually not the filter I'm worried about, I want them to be the same. It's the selected item. I just want to have them all filtered the same but with different items selected. CollectionViewSource syncs all the selections to be the same.Thanks for the info and I'll take a look at doing it the CollectionView way to see if that will work. I don't think so since I could have an arbitrary number of Views.Jason
Jason Lewis
principle is the same CollectionView tracks the selected item, as all of yours are bound to the same collection view, all have the same selected item.
Aran Mulholland
I was kind of afraid of that. Oh well, I've found a less elegant but functional way to implement what I need. Thanks for the help Aran.
Jason Lewis
try using Selector.IsSynchronizedWithCurrentItem="False", that _should_ / _maybe_ work
Aran Mulholland