Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy observable collection

Tags:

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?

like image 256
Relativity Avatar asked Nov 14 '10 20:11

Relativity


People also ask

What is observable collection?

An ObservableCollection is a dynamic collection of objects of a given type. Objects can be added, removed or be updated with an automatic notification of actions. When an object is added to or removed from an observable collection, the UI is automatically updated.

What is difference between observable collection and list?

The true difference is rather straightforward:ObservableCollection implements INotifyCollectionChanged which provides notification when the collection is changed (you guessed ^^) It allows the binding engine to update the UI when the ObservableCollection is updated. However, BindingList implements IBindingList.

What is the use of observable collection?

ObservableCollection is a collection that allows code outside the collection be aware of when changes to the collection (add, move, remove) occur. It is used heavily in WPF and Silverlight but its use is not limited to there.

Is an observable collection IEnumerable?

An IEnumerable is typically something you can enumerate - such as collections of items or generators. An ObservableCollection is a list that raises events when it's being modified. One is abstract and broad, the other concrete and specific. Note that ObservableCollection implements IEnumerable , among other interfaces.


2 Answers

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); 
like image 119
Aliostad Avatar answered Oct 09 '22 00:10

Aliostad


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); 
like image 37
Jaime Marín Avatar answered Oct 09 '22 00:10

Jaime Marín