Is there a way to do this without iterating through the List and adding the items to the ObservableCollection?
views:
6119answers:
2
+10
A:
No, there is no way to directly convert the list to an observable collection. You must add each item to the collection. However, below is a shortcut to allow the framework to enumerate the values and add them for you.
Dim list as new List(of string)
...some stuff to fill the list...
Dim observable as new ObservableCollection(of string)(list)
Nescio
2008-10-28 15:36:49
+4
A:
hi, I'm late but I want to share this interesting piece for converting a list into a ObservableCollection if you need a loop:
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;
}
You could pass an collection to the ObservableCollection constructor:
List<Product> myProds = ......
ObservableCollection<Product> oc = new ObservableCollection<Product>(myProds);
Now you have to translate these to VB.NET :)
Junior Mayhé
2009-08-25 13:29:59
That's C# not VB.NET but +1 anyway for solving it for any IEnumerable (and also for being late!)
MarkJ
2009-08-25 13:32:39
Cool joke! It is real subtle humor!!
RredCat
2010-05-28 10:27:36