Tech Off Post

Single Post Permalink

View Thread: How can I combine 2 ObservableCollections side by side?
  • Dr Herbie

    @Blunderbore: I don't think there's a built-in mechanism for this.

    Because you store the data in classes (rather than in primitives like int or double), then when you 'copy' the data to a new collection, you are not actually copying the whole data, only a reference to the data; this means that copying is less of a problem.

    If you want to combine the contents of two separate classes into a single source, then you can create a 'wrapper' class that takes two objects in it's constructor and exposes their properties as it's own. e,g,:

     

    public class Wrapper
    {
        private ClassA _a;
        private ClassB _b;
    
        public Wrapper(ClassA a, ClassB b)
        {
            _a = a;
            _b = b;
        }
    
        public double A
        {
            get
            {
                return _a.A;
            }
            set
            {
                _a.A = value;
            }
        }
    
        // ... also for properties B, C & D
    
        public double E
        {
            get
            {
                return _b.E;
            }
            set
            {
                _b.E = value;
            }
        }
    }

     

    This means you're not duplicating the original data (it's still sits in the original classes) but you can treat the wrapper class as a combined view.

    However, you are going to have to manually create a new collection of these wrapper classes to present the new data for databinding.

     

    Herbie