【问题标题】:Databindings for label on a winform usercontrolwinform用户控件上标签的数据绑定
【发布时间】:2013-04-17 11:16:11
【问题描述】:

我正在为一个包含两个标签的 winform 应用程序创建一个用户控件,一个作为标题,另一个需要通过Me.usercontrol1.databindings.add() 绑定到数据源。我是用户控件设计的新手,所以我在互联网上搜索以查找如何为我的控件制作数据绑定。我意识到我需要使用ControlBindingsCollection,但我不知道具体如何。

我找到以下代码并添加到我的用户控件中:

Private bindingContext_ As BindingContext
Private dataBindings_ As ControlBindingsCollection
Public Overrides Property BindingContext() As BindingContext
    Get
        If bindingContext_ Is Nothing Then
            bindingContext_ = New BindingContext()
        End If
        Return bindingContext_
    End Get
    Set(ByVal value As BindingContext)
        bindingContext_ = value
    End Set
End Property
<DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
Public Overloads ReadOnly Property DataBindings() As ControlBindingsCollection
    Get
        If dataBindings_ Is Nothing Then
            dataBindings_ = New ControlBindingsCollection(Me)
        End If
        Return dataBindings_
    End Get
End Property

现在我可以设置usercontrol1.databindings 参数,但显然缺少一些东西,因为我需要将此绑定的单个返回值连接到我的用户控件中的label2.Text,但我不知道如何。

有人帮我解决我的问题吗?

提前致谢。

【问题讨论】:

    标签: c# vb.net winforms data-binding user-controls


    【解决方案1】:

    我认为您不需要问题中的代码来完成这项工作。

    尝试在您的 UserControl 中创建一个使用第二个标签的属性:

    Property LabelData As String
      Get
        Return Label2.Text
      End Get
      Set(value As String)
        Label2.Text = value
      End Set
    End Property
    

    然后您的数据绑定只是映射到该属性:

    myUC.DataBindings.Add("LabelData", testObject, "Text", False, _
                          DataSourceUpdateMode.OnPropertyChanged)
    

    testObject 只是一个简单的类对象,在本例中具有 Text 属性,它实现了INotifyPropertyChanged 接口。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-29
      • 1970-01-01
      • 1970-01-01
      • 2013-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多