以下是添加到Data的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.
我也试过了:
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"))
我无法通过Google获得解决方案的类似方案。有什么想法吗?
答案 0 :(得分:1)
通过复制代码,当然还要添加这些类:
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
我像这样设置列表框:
Me.ListBox1.DataSource = bs
Me.ListBox1.DisplayMember = "Listings.Name"
结果如下:
答案 1 :(得分:0)
这里的问题是你试图使用嵌套在另一个集合中的集合作为ListBox的源,这将无法工作。
从技术上讲,如果要显示此类关系,则需要分层视觉控制,例如树。
但是,如果您只想显示内部集合(列表),那么您需要将它们从“父”类(Test)中提取出来并添加到它们自己的集合中,然后将此集合绑定到ListBox。
如果您将来需要返回父记录,那么您需要在每个列表记录中存储对它的引用(主键或其他唯一标识符)。