Faraz Azhar
Faraz Azhar

Reputation: 569

ObjectListView: Binding a list to DataListView

I'm using the DataListView (from ObjectListView) to bind a custom-object list, but I'm unable to make it work out. Here's my code:

Class MediaItem
    Property Title As String
    Property Artist As String
    Property Album As String
End Class

Dim library as New List(Of MediaItem)
dataLV.DataSource = library

Now when I add items to the List, I expected the DataListView to automatically populate itself:

library.Add(new MediaItem with {....})

But this doesn't happen. No items get created in the DataListView control. On the other hand if I populate the List first and then bind it to the DataListView, then it appears fine in the control but then again it doesn't show any changes made to the list.

Can anyone assist on what I'm missing here or whether my concept of using DataListView is incorrect?

Thanks

Upvotes: 0

Views: 1382

Answers (1)

Grammarian
Grammarian

Reputation: 6882

Your understanding of DataListView is correct. It's just your data structures that are letting you down.

First, you will need to use an ObservableCollection rather than a plain List. The ObservableCollection will fire events when items are added and removed, and the DataListView will add and remove rows accordingly.

Second, if you want to make changes to your MediaItem and have those changes automatically appear in the control, then you will have to implement the INotifyPropertyChanged interface on your MediaItem class.

There are literally dozens of places that explain these ideas. Here is a brief description: List vs ObservableCollection vs INotifyPropertyChanged

Upvotes: 3

Related Questions