vb.net 从对象内的对象列表绑定列表框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20806446/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bind listbox from a list of object inside an object
提问by user3140169
Here is a sample of 2 lists of Listing (tmp1 & tmp2) that are added to Data. Eventually this listbox will end up inside of a DataRepeater.
这是添加到数据的 2 个列表(tmp1 和 tmp2)列表的示例。最终,这个列表框将在 DataRepeater 中结束。
Dim Data As New List(Of Test)
Dim item As Test
item = New Test
Dim tmp1 As New List(Of Listing)
tmp1.Add(New Listing With {.ListingID = "004432", .Name = "Pizza Hut"})
tmp1.Add(New Listing With {.ListingID = "024235", .Name = "Houston Pizza"})
item.Listings.AddRange(tmp1)
Data.Add(item)
item = New Test
Dim tmp2 As New List(Of Listing)
tmp2.Add(New Listing With {.ListingID = "004432", .Name = "Pizza Hut"})
tmp2.Add(New Listing With {.ListingID = "024235", .Name = "Houston Pizza"})
item.Listings.AddRange(tmp2)
Data.Add(item)
Dim bs As New BindingSource
bs.DataSource = Data
ListBox1.DataSource = bs
ListBox1.DisplayMember = "Listings.Listing.Name" ' I've tried many variations.
I've also tried:
我也试过:
ListBox1.DataBindings.Add(New Binding("DisplayMember", bs, "Name"))
ListBox1.DataBindings.Add(New Binding("DisplayMember", Data, "Name"))
ListBox1.DataBindings.Add(New Binding("Items", bs, "Name"))
ListBox1.DataBindings.Add(New Binding("Items", Data, "Name"))
I've not been able to Google a similar scenario for a solution. Any ideas?
我无法在 Google 上搜索类似的解决方案。有任何想法吗?
回答by Bj?rn-Roger Kringsj?
By copying your code, and of course adding these classes:
通过复制您的代码,当然还有添加这些类:
Public Class Test
Public Sub New()
Me.m_listings = New List(Of Listing)
End Sub
Public ReadOnly Property Listings() As List(Of Listing)
Get
Return Me.m_listings
End Get
End Property
Private m_listings As List(Of Listing)
End Class
Public Class Listing
Public Sub New()
Me.m_listingID = String.Empty
Me.m_name = String.Empty
End Sub
Public Property ListingID() As String
Get
Return Me.m_listingID
End Get
Set(value As String)
Me.m_listingID = value
End Set
End Property
Public Property Name() As String
Get
Return Me.m_name
End Get
Set(value As String)
Me.m_name = value
End Set
End Property
Private m_listingID As String
Private m_name As String
End Class
I set the listbox like this:
我这样设置列表框:
Me.ListBox1.DataSource = bs
Me.ListBox1.DisplayMember = "Listings.Name"
And the result is this:
结果是这样的:


回答by competent_tech
The problem here is that you are attempting to use collections nested inside of another collection as the source for the ListBox, which won't work.
这里的问题是您试图使用嵌套在另一个集合中的集合作为 ListBox 的源,这是行不通的。
Technically, if you want to display this type of relationship, then you need a hierarchical visual control, such as a tree.
从技术上讲,如果要显示这种类型的关系,则需要一个分层的可视化控件,例如树。
However, if you just want to display the inner collections (the Listings), then you need to extract them from their "parent" class (Test) and into a collection of their own, then bind this collection to the ListBox.
但是,如果您只想显示内部集合(Listings),则需要将它们从它们的“父”类(Test)中提取出来并放入它们自己的集合中,然后将该集合绑定到 ListBox。
If you need to get back to the parent record in the future, then you will need to store a reference to it (primary key or some other unique identifier) within each Listing record.
如果以后需要返回父记录,则需要在每个列表记录中存储对它的引用(主键或某些其他唯一标识符)。

