如何为自定义控件进行数据绑定?

时间:2019-02-02 02:38:08

标签: vb.net data-binding user-controls

我正在创建一个需要为其创建数据绑定的自定义项目。

让我们考虑以下课程。为简单起见,让我们使用TextControl。 (我正在创建的真实项目不包含任何文本控件。仅用于测试)

Public Class GIce
    Implements IBindableComponent
    Implements INotifyPropertyChanged

    'The control used only for testing. (I'm avoiding making the object GIce inherit from the TextControl to make sure no interface is already implemented)
    Public TextEdit As TextEdit

    'Constructor
    Public Sub New()
        Me.TextEdit = New TextEdit
    End Sub

    'The main property to be used for data binding
    Property Content
        Get
            Return Me.TextEdit.Text
        End Get
        Set(value)
            If IsDBNull(value) Then
                Me.TextEdit.Text = ""
            Else
                Me.TextEdit.Text = value
            End If

            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("Content"))
        End Set
    End Property

    'Binding context
    Private BindingContext_ As BindingContext
    Public Property BindingContext As BindingContext Implements IBindableComponent.BindingContext
        Get
            If IsNothing(Me.BindingContext_) Then
                Me.BindingContext_ = New BindingContext
            End If

            Return Me.BindingContext_
        End Get
        Set(value As BindingContext)
            Me.BindingContext_ = value
        End Set
    End Property

    'Databindings
    Private DataBindings_ As ControlBindingsCollection
    Public ReadOnly Property DataBindings As ControlBindingsCollection Implements IBindableComponent.DataBindings
        Get
            If IsNothing(Me.DataBindings_) Then
                Me.DataBindings_ = New ControlBindingsCollection(Me)
            End If

            Return Me.DataBindings_
        End Get
    End Property

    'Property changed event
    Public Event PropertyChanged(sender As Object, e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged

    Public Property Site As System.ComponentModel.ISite Implements System.ComponentModel.IComponent.Site

    'Disposing support
    Public Event Disposed(sender As Object, e As EventArgs) Implements System.ComponentModel.IComponent.Disposed
#Region "IDisposable Support"
    ...
#End Region

    'Unimportant property. Used only for testing
    Public Property Parent
        Get
            Return TextEdit.Parent
        End Get
        Set(value)
            TextEdit.Parent = value
        End Set
    End Property

    'Unimportant property. Used only for testing
    Public Property Location
        Get
            Return TextEdit.Location
        End Get
        Set(value)
            TextEdit.Location = value
        End Set
    End Property    
End Class

然后我以这种形式使用该对象:

Public Class Form3
    Dim DataTable As DataTable      
    Dim GIce As GIce

    Public Sub New()                        
        GIce = New GIce
        GIce.parent = Me
        GIce.location = New Point(50, 50)
    End Sub

    Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        DataTable = GetDataTable() 'Basically datatable has two columns C1 and C2 and filled with string data in several rows

        'Create the binding
        Me.TextEdit1.DataBindings.Add(New Binding("Text", DataTable, "C1")) 'This control is created to make sure binging works and the datanavigator moves properly the position of the currency manager
        Me.GIce.DataBindings.Add(New Binding("Content", DataTable, "C2"))

        'Create the data navigator
        Me.DataNavigator1.DataSource = DataTable        
    End Sub
end class

执行后,自定义控件上的绑定不起作用。如果使用“数据浏览器”移动货币管理器的位置,则控件中包含的值不会更改。同样,在控件上手动键入内容时,更改也不会反映在数据表中。

有人知道为什么它不起作用吗?

0 个答案:

没有答案