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

发布于 2024-08-03 21:05:38 字数 947 浏览 6 评论 0原文

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

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。

数据绑定似乎非常适合纯文本框,但在处理更复杂的控件时似乎变得更加挑剔。

更新:我正在寻找的是绑定声明。例如。两者都……

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

不起作用。第一个被忽略(可能是因为 SelectedItem 属性为 Nothing),第二个失败并出现“无法绑定...”错误。

I have a dropdown list containing the days of the week - Monday to Sunday. It is populated with a user defined type of two values that map the numeric day of the week to it's name.

Public Structure WeekDays
   Public ID As Integer
   Public Text As String
   Public Overrides Function ToString() As String
       Return Me.Text
   End Function
End Structure

The object I want to Bind to has an integer property DayOfWeek, and I want to bind the ID value of the selected item in the dropdown to the DayOfWeek property on the Object. eg. The user selects Thursday, and the ID of 4 is passed to the object.

In code I can retrieve the UDT of the SelectedItem, but I can't work out which property on the combo box to bind to.

  1. If I add the UDTs directly to the Items collection of the dropdown, the SelectedValue is Nothing.
  2. If I add the UDTs to a List(Of UDT) collection and set that as the dropdown's datasource, with the ValueMember set to ID and DisplayMember set to Text, the SelectedValue returns the whole UDT, not the ID as instructed in the ValueMember property.

Databinding seems to work really well for plain textboxes, but it seems to get way more pernickety when dealing with more complex controls.

Update: What I am looking for is the Binding statement. eg. Neither...

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

... works. The first is just ignored (possibly because the SelectedItem property is Nothing), and the Second fails with a "Cannot bind..." error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

放我走吧 2024-08-10 21:05:38

创建属性,

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

Create Properties,

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
找个人就嫁了吧 2024-08-10 21:05:38

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

我创建了自己的 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")

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

OK, so I've found a possible solution.

I've created my own ComboBox control that inherits the standard WinForms.ComboBox and added an extra Integer property called 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

This allows me to bind to the SelectedID property...

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

...and seems to be working ok.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文