Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert a List(Of T) to an ObservableCollection(Of T) in VB.NET?

Tags:

Is there a way to do this without iterating through the List and adding the items to the ObservableCollection?

like image 363
Rob Sobers Avatar asked Oct 28 '08 15:10

Rob Sobers


People also ask

What is the difference between ObservableCollection 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.

How do you cast IEnumerable to ObservableCollection?

var myObservableCollection = new ObservableCollection<YourType>(myIEnumerable); This will make a shallow copy of the current IEnumerable and turn it in to a ObservableCollection.


1 Answers

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)
like image 115
Nescio Avatar answered Sep 18 '22 15:09

Nescio