有没有办法在CompositeCollection的当前位置改变时得到通知?
我需要有一个CollectionView监控的CompositeCollection,任何想法都是欢迎的。
发布于 2010-01-17 09:00:53
您可以通过监视CollectionView的ICollectionView.CurrentChanged事件来检测当前项何时发生更改。以下代码适用于我:
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"当我在ListBox中更改选择时,将显示消息框。
关于过滤、排序和分组,根据Aron的回答,这些在CompositeCollection上的视图中是不可用的。但需要注意的是,您可以通过以下方式来检测支持这些特性的视图的更改:
当过滤器改变时,看起来你会得到一个CollectionChanged事件,尽管我找不到这个documented.
ObservableCollection<GroupDescription>上挂接一个INotifyCollectionChanged事件处理程序,所以在GroupDescriptions属性上挂接一个GroupDescriptions事件处理程序。发布于 2010-01-17 09:42:50
不能在复合集合上运行CollectionView,请参见here
发布于 2016-07-12 21:01:30
我遇到了同样的问题:我需要对CompositeCollection进行排序。我写了下面的类来解决这个问题,至少对于相同类型的ObservableCollections是这样的。
其思想是将复合集合作为普通的可观察集合进行维护,并在底层集合发生变化时对其进行更新。然后,可以在用户界面中使用得到的集合(AllNodes),并且它可以很好地支持CollectionView。
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
namespace Util {
public class ObservableCollectionCollector<T> {
private class ReplacableObservableCollection : ObservableCollection<T> {
public void Replace(int idx, T v) {
SetItem(idx, v);
}
}
private readonly ReplacableObservableCollection allNodes;
private readonly ObservableCollection<T>[] colls;
private readonly int[] lens;
public ObservableCollectionCollector(params ObservableCollection<T>[] colls) {
this.colls = colls;
allNodes = new ReplacableObservableCollection();
foreach (var l in colls) {
foreach (var e in l)
allNodes.Add(e);
l.CollectionChanged += HandleCollectionChanged;
}
lens = colls.Select(c => c.Count).ToArray();
}
public ReadOnlyObservableCollection<T> AllNodes {
get { return new ReadOnlyObservableCollection<T>(allNodes); }
}
private void HandleCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) {
int i0 = 0;
int ci = 0;
foreach (var l in colls) {
if (l == sender)
break;
i0 += l.Count;
++ci;
}
switch (e.Action) {
case NotifyCollectionChangedAction.Add:
for (int i = 0; i < e.NewItems.Count; ++i)
allNodes.Insert(i0 + e.NewStartingIndex + i, (T)e.NewItems[i]);
break;
case NotifyCollectionChangedAction.Move:
for (int i = 0; i < e.OldItems.Count; ++i)
allNodes.Move(i0 + e.OldStartingIndex + i, i0 + e.NewStartingIndex + i);
break;
case NotifyCollectionChangedAction.Remove:
for (int i = 0; i < e.OldItems.Count; ++i)
allNodes.RemoveAt(i0 + e.OldStartingIndex);
break;
case NotifyCollectionChangedAction.Replace:
for (int i = 0; i < e.NewItems.Count; ++i)
allNodes.Replace(i0 + e.OldStartingIndex + i, (T)e.NewItems[i]);
break;
case NotifyCollectionChangedAction.Reset:
for (int i = 0; i < lens[ci]; ++i)
allNodes.RemoveAt(i0);
break;
}
lens[ci] = ((ObservableCollection<T>)sender).Count;
}
}
}https://stackoverflow.com/questions/2079553
复制相似问题