2012-08-01 54 views
0

將綁定控件更新爲對象我有一個對象,其屬性也是對象並綁定到文本框。我希望能夠設置MyOriginalObj = MyNewOject並讓文本框顯示新值。這不起作用。我必須做的是MyOriginalObj.PropertyA = MyNewObj.PropertyA。這將導致文本框更新。如何通過設置ObjectA = ObjectB

我想避免後一種方法,因爲我的實際類具有比我下面的測試類的很多屬性,並會增加做所有的更新所需的代碼。如果我不得不我想我可以解除綁定並綁定到新的對象,但又是添加更多的代碼。 MyOriginalObj = MyNewObj方法將是最簡單的解決方案,但我不確定是否有可能。請指教。

Dim fam As New Family 

Public Class Person 

    Dim _Age As Integer = 0 
    Dim _Name As String = "" 

    Public Property Age() As Integer 
     Get 
      Return _Age 
     End Get 
     Set(ByVal value As Integer) 
      _Age = value 
     End Set 
    End Property 

    Public Property Name() As String 
     Get 
      Return _Name 
     End Get 
     Set(ByVal value As String) 
      _Name = value 
     End Set 
    End Property 

    Public Sub New(ByVal age As Integer, ByVal name As String) 

     With Me 
      ._Age = age 
      ._Name = name 
     End With 

    End Sub 

End Class 

-

Public Class Family 

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

    Dim _Father As New Person(0, "") 
    Dim _Mother As New Person(0, "") 

    Public Property Father() As Person 
     Get 
      Return _Father 
     End Get 
     Set(ByVal value As Person) 
      _Father = value 
      RaiseEvent PropertyChanged(Me, 
       New System.ComponentModel.PropertyChangedEventArgs("Father")) 
     End Set 
    End Property 

    Public Property Mother() As Person 
     Get 
      Return _Mother 
     End Get 
     Set(ByVal value As Person) 
      _Mother = value 
      RaiseEvent PropertyChanged(Me, 
       New System.ComponentModel.PropertyChangedEventArgs("Mother")) 
     End Set 
    End Property 

End Class 

-

Private Sub Form1_Load(ByVal sender As System.Object, 
         ByVal e As System.EventArgs) Handles MyBase.Load 

    TextBox1.DataBindings.Add("Text", fam, "Father.Name") 
    TextBox2.DataBindings.Add("Text", fam, "Mother.Name") 

End Sub 

-

Private Sub Button1_Click(ByVal sender As System.Object, 
          ByVal e As System.EventArgs) Handles Button1.Click 

    ' Method A - works, textboxes displays the new values. 
    ' fam.Father = New Person(30, "Joe") 
    ' fam.Mother = New Person(29, "Jane") 
    ' Exit Sub 

    ' Method B - does not update textboxes. 
    Dim fam2 As New Family 
    fam2.Father = New Person(40, "Bob") 
    fam2.Mother = New Person(39, "Betty") 

    ' I would like the updated properties to be shown in the bound 
    ' textboxes when I set fam = fam2. Object fam will contain 
    ' the new values but the textboxes will not reflect that. 
    fam = fam2 
    Console.WriteLine("{0},{1} : {2}, {3}", _ 
         fam.Father.Name, _ 
         fam.Father.Age, _ 
         fam.Mother.Name, _ 
         fam.Mother.Age) 

End Sub 

回答

0

你可以實現你的family類中的方法,將實現你在找什麼。

Public Class Family 

    . . . 

    Public Sub ReplaceFamily(newFamily As Family) 
    With newFamily 
     Me.Father = .Father 
     Me.Mother = .Mother 
    End With 
    End Sub 

End Class 

您的通話將如下所示:

fam.ReplaceFamily(fam2) 

如果您在family有很多的屬性,你可以使用反射的ReplaceFamily方法中,而不是單獨設置每個屬性的。

+0

我的最終目標是從磁盤加載和反序列化xml文件。在應用程序啓動時,我將使用默認值創建一個Family實例,然後將該實例綁定到控件。然後,無論何時加載新的xml文件,我都會將新的Family對象傳遞給ReplaceFamily方法,並且綁定的控件將反映新的數據。一旦我找出實現反射的附加代碼,使其工作應該是最小的。謝謝! – 2012-08-04 16:51:36

+0

@SteveZuranski,祝你的項目好運 – 2012-08-04 17:18:45