将Combobox绑定到对象数据源

时间:2012-10-18 19:29:46

标签: vb.net data-binding

我有一个绑定到对象数据源的表单。它有一个文本框和一个组合框。我为主对象设置了一个绑定源,为组合框设置了一个绑定源。当我运行该窗体时,文本框被正确绑定,并且组合框中的值列表正确绑定,但组合框的ValueMember无法正常工作。

组合框显示正确的列表,但它的选择索引是0而不是它应该是2.当我更改文本框中的值时,它的绑定对象的Property.Set方法被正确调用,但是相同的属性不为组合框调用.Set方法。

我知道我可以在表单中修改OnSelectedIndex更改方法,但我想知道在使用Bindings时我做错了什么。

以下是表单上的代码:

Public Class Form1
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    Dim NameValueBindingSource1 As New BindingSource()
    Dim WorkOrderBindingSource1 As New BindingSource

    'Create main object to bind to
    Dim wo As New WorkOrder
    wo.WOIndex = "2012-0111"
    wo.WorkOrderType = 3

    'Create list object for combo box
    Dim NameValues As BindingList(Of NameValue)
    NameValues = FillNameValueList()

    'Bind Text Box to Binding Source
    WorkOrderBindingSource1.DataSource = wo
    WOIndexTextBox1.DataBindings.Add("Text", WorkOrderBindingSource1, "WOIndex")

    'Bind Combo Box to Binding Source
    NameValueBindingSource1.DataSource = NameValues
    WorkOrderTypeCombo.DataSource = NameValueBindingSource1
    WorkOrderTypeCombo.DisplayMember = "Value"
    WorkOrderTypeCombo.ValueMember = "Code"

End Sub

Function FillNameValueList() As BindingList(Of NameValue)
    Dim bl As New BindingList(Of NameValue)
    Dim nv As NameValue

    nv = New NameValue
    bl.Add(New NameValue("Short", 0))
    bl.Add(New NameValue("Middle", 1))
    bl.Add(New NameValue("Long", 2))
    bl.Add(New NameValue("Very Long", 3))

    Return bl
End Function
End Class

这是主要对象的代码 - “WorkOrder”

Imports System.ComponentModel
Public Class WorkOrder
    Implements IEditableObject
    Implements INotifyPropertyChanged

    Private mWOIndex As String
    Private mWorkOrderType As Integer

    Public Event PropertyChanged(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged

    Public Property WOIndex As String
        Get
            Return mWOIndex
        End Get
        Set(value As String)
            mWOIndex = value
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("WOIndex"))
        End Set
    End Property
    Public Property WorkOrderType As Integer
        Get
            Return mWorkOrderType
        End Get
        Set(value As Integer)
            mWorkOrderType = value
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("WorkOrderType"))
        End Set
    End Property
    Public Sub BeginEdit() Implements System.ComponentModel.IEditableObject.BeginEdit
    End Sub
    Public Sub CancelEdit() Implements System.ComponentModel.IEditableObject.CancelEdit
    End Sub
    Public Sub EndEdit() Implements System.ComponentModel.IEditableObject.EndEdit
    End Sub
End Class

这是组合框中使用的对象的代码

Imports System.ComponentModel

Public Class NameValue
    Implements IEditableObject
    Implements INotifyPropertyChanged

    Private mValue As String
    Private mCode As Integer

    Public Event PropertyChanged(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged

    Public Property Code As Integer
        Get
            Return mCode
        End Get
        Set(value As Integer)
            mCode = value
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("Code"))
        End Set
    End Property
    Public Property Value As String
        Get
            Return mValue
        End Get
        Set(value As String)
            mValue = value
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("Value"))
        End Set
    End Property

    Public Sub BeginEdit() Implements System.ComponentModel.IEditableObject.BeginEdit
    End Sub
    Public Sub CancelEdit() Implements System.ComponentModel.IEditableObject.CancelEdit
    End Sub
    Public Sub EndEdit() Implements System.ComponentModel.IEditableObject.EndEdit
    End Sub
    Public Sub New(InitValue As String, InitCode As Integer)
        Value = InitValue
        Code = InitCode
    End Sub
End Class

1 个答案:

答案 0 :(得分:1)

在您的代码中,您只是将DataSource分配给ComboBox,但您没有为它建立任何DataBinding。

你需要这样一行(在这里使用C#):

WorkOrderTypeCombo.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", WorkOrderBindingSource1, "WorkOrderType", true));

希望这有帮助