2013-04-24 61 views
0

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

我发现下面的代码,并添加到我的用户:

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在我的用户和连接我不知道如何。

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

在此先感谢。

回答

1

我不认为你需要在你的问题的代码,使这项工作。

尝试制作了使用性能,在你的用户控件第二个标签:

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接口。

相关问题