开发者

Can you turn off selection syncing in WPF's CollectionViewSource?

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 ha开发者_开发百科ving is that CollectionViewSource forces them to all have the same item selected. Is there some way to turn this off?

Thanks, Jason Lewis


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜