.net 如何数据绑定到用户定义类型的下拉列表?

.net 如何数据绑定到用户定义类型的下拉列表?,.net,winforms,data-binding,business-objects,.net,Winforms,Data Binding,Business Objects,我有一个下拉列表,包含一周中的几天——周一到周日。它由两个值组成的用户定义类型填充,这两个值将一周中的数字日期映射到它的名称 Public Structure WeekDays Public ID As Integer Public Text As String Public Overrides Function ToString() As String Return Me.Text End Function End Structure 我要绑定到的对象具

我有一个下拉列表,包含一周中的几天——周一到周日。它由两个值组成的用户定义类型填充,这两个值将一周中的数字日期映射到它的名称

Public Structure WeekDays
   Public ID As Integer
   Public Text As String
   Public Overrides Function ToString() As String
       Return Me.Text
   End Function
End Structure
我要绑定到的对象具有整数属性DayOfWeek,我要将下拉列表中选定项的ID值绑定到对象的DayOfWeek属性。例如,用户选择星期四,将ID 4传递给对象

在代码中,我可以检索SelectedItem的UDT,但无法确定要绑定到组合框上的哪个属性

  • 如果将UDT直接添加到下拉列表的Items集合中,则SelectedValue为Nothing
  • 如果我将UDT添加到列表(UDT)集合中,并将其设置为下拉列表的数据源,ValueMember设置为ID,DisplayMember设置为Text,则SelectedValue将返回整个UDT,而不是ValueMember属性中指示的ID
  • 数据绑定似乎对普通文本框非常有效,但在处理更复杂的控件时,它似乎变得更加有害

    更新:我要找的是绑定语句。都不是

    oB = New Binding("SelectedItem", Payroll, "DayOfWeek")
    oB = New Binding("SelectedItem.ID", Payroll, "DayOfWeek")
    
    。。。作品第一个被忽略(可能是因为SelectedItem属性为Nothing),第二个失败,出现“无法绑定…”错误。

    创建属性

    Public Structure WeekDays
        Private _ID As Integer
        Private _Text As String
        Public Sub New(ByVal ID As Integer, ByVal Text As String)
            Me._ID = ID
            Me._Text = Text
        End Sub
        Public Overrides Function ToString() As String
            Return Me._Text
        End Function
    
        Public Property ID() As Integer
            Get
                Return _ID
            End Get
            Set(ByVal value As Integer)
                _ID = value
            End Set
        End Property
        Public Property Text() As String
            Get
                Return _Text
            End Get
            Set(ByVal value As String)
                _Text = value
            End Set
        End Property
    End Structure
    
    
    Public Class Form1
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim items As New List(Of WeekDays)
    
            items.Add(New WeekDays(1, "A"))
            items.Add(New WeekDays(2, "B"))
    
            Dim lb As New ListBox
            lb.DataSource = items
            lb.ValueMember = "ID"
            lb.DisplayMember = "Text"
            AddHandler lb.SelectedIndexChanged, AddressOf Item_Sel
    
            Me.Controls.Add(lb)
    
            TextBox1.DataBindings.Add(New Binding("Text", items, "Text"))
    
            Dim cb As New ComboBox
            cb.DataSource = items
            cb.DisplayMember = "Text"
            cb.ValueMember = "ID"
            cb.DataBindings.Add("SelectedValue", items, "ID")
            cb.Location = New Point(100, 100)
            Me.Controls.Add(cb)
            TextBox1.DataBindings.Add(New Binding("Text", items, "ID"))           
        End Sub
    
        Public Sub Item_Sel(ByVal sender As System.Object, ByVal e As System.EventArgs)
            Dim obj As Object = CType(sender, ListBox).SelectedValue
            MsgBox(obj)
        End Sub
    End Class
    

    好的,我找到了一个可能的解决办法

    我创建了自己的ComboBox控件,该控件继承了标准的WinForms.ComboBox,并添加了一个名为SelectedID的额外整数属性

    Public Structure NumericUDT
       Public ID As Integer
       Public Text As String
    
       Public Sub New(ByVal iID As Integer, ByVal sText As String)
           Me.ID = iID
           Me.Text = sText
       End Sub
       Public Overrides Function ToString() As String
           Return Me.Text
       End Function
    End Structure
    
    Public Property SelectedID() As Integer
        Get
            Dim uItem As NumericUDT
            Dim iID As Integer
    
            If (MyBase.SelectedItem Is Nothing) Then
                iID = 0
            Else
                uItem = DirectCast(MyBase.SelectedItem, NumericUDT)
                iID = uItem.ID
            End If
    
            Return iID
    
        End Get
        Set(ByVal value As Integer)
    
            Dim uItem As NumericUDT
            Dim uFound As NumericUDT = Nothing
    
            For Each uItem In MyBase.Items
                If uItem.ID = value Then
                    uFound = uItem
                    Exit For
                End If
            Next
    
            MyBase.SelectedItem = uFound
    
        End Set
    End Property
    
    这允许我绑定到SelectedID属性

       oB = New Binding("SelectedID", Payroll, "PayDay")
    

    …而且似乎工作正常。

    Hmmm.我不明白为什么一个属性的工作方式会与公共字段有任何不同,但我还是尝试了这一点。在它不起作用之后,我试着从UDT改为类,但也不起作用。我认为您的示例之所以有效,是因为您绑定到了Text属性(它恰好同时存在于combo和UDT中)。我需要能够绑定到ID值,但是DataBindings.Add操作无法在组合上找到ID属性并导致错误。不,它与组合框属性无关。您可以将ValueMember和DisplayMember设置为数据项的任何公共属性。我将等待几天,看看是否有更好的解决方案,否则我将接受此答案。