I am in wpf, and have a generic list: List. Now I wish to cast it to a generic observable collections: ObservableCollection.
I understand I can iterate over the list and
If you JUST want to create an ObservableCollection
from a List
, then all you need to do is
ObservableCollection<MyType> obsCollection = new ObservableCollection<MyType>(myList);
you can do it by using extension method
public static ObservableCollection<T> ToObservableCollection<T>(this IEnumerable<T> coll)
{
var c = new ObservableCollection<T>();
foreach (var e in coll) c.Add(e);
return c;
}
or you can use this constructor The elements are copied onto the ObservableCollection in the same order they are read by the enumerator of the list.
ObservableCollection<YourObject> collection = new ObservableCollection<YourObject>(yourList);
ObservableCollection has Conttructor for IEnumerable<T>
ObservableCollection
ObservableCollection<yourType> observable =
new ObservableCollection<yourType>(yourListObject);
var _oc = new ObservableCollection<ObjectType>(_listObjects);