18

Have a bunch of ObservableCollection<MeClass> Result and require to combine them all into another ObservableCollection<MeClass> AllResults so I can display it in a listview.

Just not sure how to combine them all in one.

I Created a new class to combine them all but not sure how they will get updated after I got the list once... So not really sure which direction to take.

I know about INotifyPropertyChanged I'm just not sure how to combine them all and keep updating as everything changes.

Adi Lester
  • 24,731
  • 12
  • 95
  • 110
Reza M.
  • 1,205
  • 14
  • 33

3 Answers3

30

.NET has a CompositeCollection that allows you to treat multiple collections as a single collection. It implements INotifyCollectionChanged, so as long as your inner collections implement INotifyCollectionChanged as well (which in your case they certainly do), your bindings should work without any problems.

Usage example:

CompositeCollection cc = new CompositeCollection();
CollectionContainer container1 = new CollectionContainer() { Collection = Result1 }
CollectionContainer container2 = new CollectionContainer() { Collection = Result2 }
cc.Add(container1);
cc.Add(container2);
Adi Lester
  • 24,731
  • 12
  • 95
  • 110
  • 4
    Note that you cannot use grouping with `CompositeCollectionView`. It's `CanGroup` is `false`, and its `ICollectionView.GroupDescriptions` property is `null` and is non-settable. – Drew Noakes May 23 '17 at 13:07
4

Something like this?

public class CompositeCollection : ObservableCollection<MeClass>
{
    private ObservableCollection<MeClass> _subCollection1;
    private ObservableCollection<MeClass> _subCollection2;

    public CompositeCollection(ObservableCollection<MeClass> subCollection1, ObservableCollection<MeClass> subCollection2) 
    {
        _subCollection1 = subCollection1;
        _subCollection2 = subCollection2;

        AddSubCollections();
        SubscribeToSubCollectionChanges();
    }

    private void AddSubCollections()
    {
        AddItems(_subCollection1.All);
        AddItems(_subCollection2.All);
    }

    private void AddItems(IEnumerable<MeClass> items)
    {
        foreach (MeClass me in items)
            Add(me);
    }

    private void RemoveItems(IEnumerable<MeClass> 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<MeClass>());
                                                       break;

            case NotifyCollectionChangedAction.Remove: RemoveItems(args.OldItems.Cast<MeClass>());
                                                       break;

            case NotifyCollectionChangedAction.Reset:  Clear();
                                                       AddSubCollections();
                                                       break;
        }
    }
}
GazTheDestroyer
  • 20,722
  • 9
  • 70
  • 103
  • 2
    No need to reinvent the wheel - .NET already has a CompositeCollection (see my answer). – Adi Lester Nov 03 '12 at 01:12
  • 2
    This approach works with grouping, unlike the framework's built-in `CompositeCollection`. Thanks. BTW the `All` symbols in `AddSubCollections` are compile errors. – Drew Noakes May 23 '17 at 14:01
4

I reworked @GazTheDestroyer's answer into this (requires C# 7):

internal sealed class CompositeObservableCollection<T> : ObservableCollection<T>
{
    public CompositeObservableCollection(INotifyCollectionChanged subCollection1, INotifyCollectionChanged subCollection2)
    {
        AddItems((IEnumerable<T>)subCollection1);
        AddItems((IEnumerable<T>)subCollection2);

        subCollection1.CollectionChanged += OnSubCollectionChanged;
        subCollection2.CollectionChanged += OnSubCollectionChanged;

        void OnSubCollectionChanged(object source, NotifyCollectionChangedEventArgs args)
        {
            switch (args.Action)
            {
                case NotifyCollectionChangedAction.Add:
                    AddItems(args.NewItems.Cast<T>());
                    break;
                case NotifyCollectionChangedAction.Remove:
                    RemoveItems(args.OldItems.Cast<T>());
                    break;
                case NotifyCollectionChangedAction.Reset:
                    Clear();
                    AddItems((IEnumerable<T>)subCollection1);
                    AddItems((IEnumerable<T>)subCollection2);
                    break;
                case NotifyCollectionChangedAction.Replace:
                    RemoveItems(args.OldItems.Cast<T>());
                    AddItems(args.NewItems.Cast<T>());
                    break;
                case NotifyCollectionChangedAction.Move:
                    throw new NotImplementedException();
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }

        void AddItems(IEnumerable<T> items)
        {
            foreach (var me in items)
                Add(me);
        }

        void RemoveItems(IEnumerable<T> items)
        {
            foreach (var me in items)
                Remove(me);
        }
    }
}
Drew Noakes
  • 300,895
  • 165
  • 679
  • 742