问题描述
我正在使用DataListView(来自ObjectListView)来绑定自定义对象列表,但是我无法使其解决.这是我的代码:
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
现在,当我向列表中添加项目时,我希望DataListView能够自动填充自身:
Now when I add items to the List, I expected the DataListView to automatically populate itself:
library.Add(new MediaItem with {....})
但这不会发生.在DataListView控件中没有创建任何项目.另一方面,如果我先填充列表,然后将其绑定到DataListView,则它在控件中看起来不错,但随后它又不显示对该列表所做的任何更改.
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.
任何人都可以协助我解决这里遗漏的问题,或者我使用DataListView的概念是否不正确?
Can anyone assist on what I'm missing here or whether my concept of using DataListView is incorrect?
谢谢
推荐答案
您对 DataListView
的理解是正确的.只是您的数据结构让您失望.
Your understanding of DataListView
is correct. It's just your data structures that are letting you down.
首先,您需要使用 ObservableCollection
,而不是简单的 List
. ObservableCollection
将在添加和删除项目时触发事件,而 DataListView
将相应地添加和删除行.
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.
第二,如果要更改 MediaItem
并将这些更改自动显示在控件中,则必须在 MediaItem
类.
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.
确实有很多地方可以解释这些想法.以下是简要说明:列表vs ObservableCollection vs INotifyPropertyChanged
There are literally dozens of places that explain these ideas. Here is a brief description: List vs ObservableCollection vs INotifyPropertyChanged
这篇关于ObjectListView:将列表绑定到DataListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!