2013-04-24 14 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接口。