1

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

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,但无法确定要绑定到组合框上的哪个属性。

  1. 如果我将 UDT 直接添加到下拉列表的 Items 集合中,则 SelectedValue 为 Nothing。
  2. 如果我将 UDT 添加到 List(Of UDT) 集合并将其设置为下拉列表的数据源,并将 ValueMember 设置为 ID 并将 DisplayMember 设置为 Text,则 SelectedValue 返回整个 UDT,而不是 ValueMember 属性中指示的 ID。

数据绑定似乎对纯文本框非常有效,但在处理更复杂的控件时似乎变得更加棘手。

更新:我正在寻找的是 Binding 语句。例如。两者都不...

oB = New Binding("SelectedItem", Payroll, "DayOfWeek")
oB = New Binding("SelectedItem.ID", Payroll, "DayOfWeek")

...有效。第一个被忽略(可能是因为 SelectedItem 属性为 Nothing),第二个因“无法绑定...”错误而失败。

4

2 回答 2

1

好的,所以我找到了一个可能的解决方案。

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

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")

...并且似乎工作正常。

于 2009-09-05T22:59:00.127 回答
1

创建属性,

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
于 2009-09-05T15:30:00.717 回答