如何使用CollectionView功能处理CompositeCollection? [英] How to handle a CompositeCollection with CollectionView features?
问题描述
当CompositeCollection的当前位置更改时,是否有办法得到通知?
Is there a way to get notified when CompositeCollection's current location changes?
我需要由CollectionView监视CompositeCollection,欢迎提出任何想法.
I need to have the CompositeCollection monitored by a CollectionView, any ideas are welcommed.
推荐答案
您可以通过监视CollectionView的ICollectionView.CurrentChanged
事件来检测当前项何时已更改.以下代码对我有用:
You can detect when the current item has changed by monitoring the ICollectionView.CurrentChanged
event of your CollectionView. The following code works for me:
CompositeCollection cc = new CompositeCollection();
cc.Add(new CollectionContainer { Collection = new string[] { "Oh No!", "Fie" } });
cc.Add(new CollectionContainer { Collection = new string[] { "Zounds", "Ods Bodikins" } });
CollectionViewSource cvs = new CollectionViewSource { Source = cc };
// Subscribing to CurrentChanged on the ICollectionView
cvs.View.CurrentChanged += (o, e) => MessageBox.Show("current changed");
lb.ItemsSource = cvs.View; // lb is a ListBox with IsSynchronizedWithCurrentItem="True"
当我在列表框中更改选择时,将显示消息框.
When I change the selection in the ListBox, the message box displays.
关于过滤,排序和分组,按照Aron的回答,这些在CompositeCollection的视图上不可用.但是,为了记录在案,您可以通过以下方法检测对做支持这些功能的视图的更改:
Regarding filtering, sorting and grouping, as per Aron's answer these are not available on a view over a CompositeCollection. But for the record here are the ways you can detect changes for views that do support these features:
- 尽管我找不到该文档,但看起来当过滤器更改时您会收到一个CollectionChanged事件.
- SortDescriptions是SortDescriptionCollection,它是INotifyCollectionChanged,因此请在SortDescriptions属性上连接CollectionChanged事件处理程序.
- GroupDescriptions是
ObservableCollection<GroupDescription>
,因此请在GroupDescriptions属性上连接CollectionChanged事件处理程序.
- It looks like you'll get a CollectionChanged event when the filter changes, though I can't find this documented.
- SortDescriptions is SortDescriptionCollection which is INotifyCollectionChanged, so hook up a CollectionChanged event handler on the SortDescriptions property.
- GroupDescriptions is
ObservableCollection<GroupDescription>
, so hook up a CollectionChanged event handler on the GroupDescriptions property.
这篇关于如何使用CollectionView功能处理CompositeCollection?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!