23

I have

Observablecollection<A> aRef = new Observablecollection<A>();
bRef = aRef(); 

In this case both point to same ObservableCollection. How do I make a different copy?

Abra
  • 19,142
  • 7
  • 29
  • 41
Relativity
  • 6,690
  • 22
  • 78
  • 128

2 Answers2

33

Do this:

// aRef being an Observablecollection 
Observablecollection<Entity> bRef = new Observablecollection<Entity>(aRef);

This will create an observable collection but the items are still pointing to the original items. If you need the items to point a clone rather than the original items, you need to implement and then call a cloning method.

UPDATE

If you try to add to a list and then the observable collection have the original list, just create the Observablecollection by passing the original list:

List<Entity> originalEnityList = GetThatOriginalEnityListFromSomewhere();
Observablecollection<Entity> bRef = new Observablecollection<Entity>(originalEnityList);
Aliostad
  • 80,612
  • 21
  • 160
  • 208
  • Thanks...But my real issue is I have an observable collection...when i add a new item to it..it's not getting reflected in UI. So what I am doing now is copy whole collection to a temperory collection...then add new item to temp collection. Then assign back the temp collection back. – Relativity Nov 14 '10 at 20:53
  • 1
    But I have observable collection as input. Ie....I have an observable collection of 120 items..I add new item to it..now it has 121 items...Then UI should display 121 items – Relativity Nov 14 '10 at 21:01
  • SO there is not a way. Observablecollection does not expose the original list. You have to expose the original list mate. – Aliostad Nov 14 '10 at 21:03
  • Can we use "All", "Select" methods to get a new observable collection ? – Relativity Nov 14 '10 at 21:04
  • The point is that you are adding to the original list and you want the new OC to show your changes. All or Select will create a new list. But why do you want to use a new OC anyway? Just bind the old OC to a new control, that is all you seem to need. – Aliostad Nov 14 '10 at 21:12
  • you have to use two way binding so that if there is any change in the original observable collection then it will be reflected. – Dragon May 08 '14 at 11:38
11

You could implement ICloneable interface in you entity definition and then make a copy of the ObservableCollection with a internal cast. As a result you will have a cloned List without any reference to old items. Then you could create your new ObservableCollection whit the cloned List

public class YourEntity : ICloneable {
    public AnyType Property { get; set; }
    ....
    public object Clone()
    {
        return MemberwiseClone();
    }
}

The implementation would be

var clonedList = originalObservableCollection.Select(objEntity => (YourEntity) objEntity.Clone()).ToList();

ObservableCollection<YourEntity> clonedCollection = new ObservableCollection<YourEntity>(clonedList);
Jaime Marín
  • 578
  • 6
  • 11
  • That is not working when originalObservableCollection is binded. When you change something in it, clonedCollection get's changes too. – Lucy82 Apr 20 '21 at 18:32