将多个ObservableCollections绑定到One ObservableCollection

有一堆ObservableCollection Result并要求将它们全部组合到另一个ObservableCollection AllResults这样我就可以在listview显示它。

只是不确定如何将它们合二为一。

我创建了一个新类,将它们组合在一起,但不确定在我获得列表后它们将如何更新…所以不确定要采取哪个方向。

我知道关于INotifyPropertyChanged我只是不确定如何将它们全部组合并随着一切变化而不断更新。

.NET有一个CompositeCollection ,允许您将多个集合视为一个集合。 它实现了INotifyCollectionChanged ,因此只要您的内部集合也实现了INotifyCollectionChanged (在您的情况下它们确实如此),您的绑定应该没有任何问题。

用法示例:

 CompositeCollection cc = new CompositeCollection(); CollectionContainer container1 = new CollectionContainer() { Collection = Result1 } CollectionContainer container2 = new CollectionContainer() { Collection = Result2 } cc.Add(container1); cc.Add(container2); 

像这样的东西?

 public class CompositeCollection : ObservableCollection { private ObservableCollection _subCollection1; private ObservableCollection _subCollection2; public CompositeCollection(ObservableCollection subCollection1, ObservableCollection subCollection2) { _subCollection1 = subCollection1; _subCollection2 = subCollection2; AddSubCollections(); SubscribeToSubCollectionChanges(); } private void AddSubCollections() { AddItems(_subCollection1.All); AddItems(_subCollection2.All); } private void AddItems(IEnumerable items) { foreach (MeClass me in items) Add(me); } private void RemoveItems(IEnumerable items) { foreach (MeClass me in items) Remove(me); } private void SubscribeToSubCollectionChanges() { _subCollection1.CollectionChanged += OnSubCollectionChanged; _subCollection2.CollectionChanged += OnSubCollectionChanged; } private void OnSubCollectionChanged(object source, NotifyCollectionChangedEventArgs args) { switch(args.Action) { case NotifyCollectionChangedAction.Add: AddItems(args.NewItems.Cast()); break; case NotifyCollectionChangedAction.Remove: RemoveItems(args.OldItems.Cast()); break; case NotifyCollectionChangedAction.Reset: Clear(); AddSubCollections(); break; } } } 

我重写了@ GazTheDestroyer对此的回答(需要C#7):

 internal sealed class CompositeObservableCollection : ObservableCollection { public CompositeObservableCollection(INotifyCollectionChanged subCollection1, INotifyCollectionChanged subCollection2) { AddItems((IEnumerable)subCollection1); AddItems((IEnumerable)subCollection2); subCollection1.CollectionChanged += OnSubCollectionChanged; subCollection2.CollectionChanged += OnSubCollectionChanged; void OnSubCollectionChanged(object source, NotifyCollectionChangedEventArgs args) { switch (args.Action) { case NotifyCollectionChangedAction.Add: AddItems(args.NewItems.Cast()); break; case NotifyCollectionChangedAction.Remove: RemoveItems(args.OldItems.Cast()); break; case NotifyCollectionChangedAction.Reset: Clear(); AddItems((IEnumerable)subCollection1); AddItems((IEnumerable)subCollection2); break; case NotifyCollectionChangedAction.Replace: RemoveItems(args.OldItems.Cast()); AddItems(args.NewItems.Cast()); break; case NotifyCollectionChangedAction.Move: throw new NotImplementedException(); default: throw new ArgumentOutOfRangeException(); } } void AddItems(IEnumerable items) { foreach (var me in items) Add(me); } void RemoveItems(IEnumerable items) { foreach (var me in items) Remove(me); } } }