当整个对象发生变化时保持双向绑定
我有一个类:
Public Class TestClass
Implements INotifyPropertyChanged
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Public Sub OnNotifyChanged(ByVal pName As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(pName))
End Sub
Private _One As Integer
Private _Two As Integer
Public Sub New(ByVal One As Integer, ByVal Two As Integer)
_One = One
_Two = Two
End Sub
Public Property One() As Integer
Get
Return _One
End Get
Set(ByVal value As Integer)
_One = value
OnNotifyChanged("One")
End Set
End Property
Public Property Two() As Integer
Get
Return _Two
End Get
Set(ByVal value As Integer)
_Two = value
OnNotifyChanged("Two")
End Set
End Property
End Class
我可以创建一个实例并将两个文本框绑定到该对象:
Dim MyObject As New TestClass(1, 2)
TextBoxOne.DataBindings.Add("Text", MyObject, "One")
TextBoxTwo.DataBindings.Add("Text", MyObject, "Two")
现在我可以更改文本框或对象:
MyObject.Two = 3
..对象和文本框以两种方式更新。
现在我想更新整个对象:
MyObject = New TestClass(3, 4)
...但这不会更新文本框。
我做错了什么?
I have a class:
Public Class TestClass
Implements INotifyPropertyChanged
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Public Sub OnNotifyChanged(ByVal pName As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(pName))
End Sub
Private _One As Integer
Private _Two As Integer
Public Sub New(ByVal One As Integer, ByVal Two As Integer)
_One = One
_Two = Two
End Sub
Public Property One() As Integer
Get
Return _One
End Get
Set(ByVal value As Integer)
_One = value
OnNotifyChanged("One")
End Set
End Property
Public Property Two() As Integer
Get
Return _Two
End Get
Set(ByVal value As Integer)
_Two = value
OnNotifyChanged("Two")
End Set
End Property
End Class
I can create an instance and bind two TextBoxes to the object:
Dim MyObject As New TestClass(1, 2)
TextBoxOne.DataBindings.Add("Text", MyObject, "One")
TextBoxTwo.DataBindings.Add("Text", MyObject, "Two")
Now I can change the TextBoxes or the object:
MyObject.Two = 3
..the object and TextBoxes are updated in two ways.
Now I want to update the whole object:
MyObject = New TestClass(3, 4)
...but this doesn't update the TextBoxes.
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的文本框包含对您创建的对象的第一个实例的引用。
现在您正在创建第二个实例,据说是为了替换现有实例,但文本框不知道更改。
您需要:
将新实例传递到文本框(直接传递给第一个实例,或者通过两个框都绑定到的某个“模型”对象间接传递)。
更新现有实例而不是用新实例替换它(您可以简单地为字段分配值,或创建一些“AssignFrom(其他)”方法等)
为自己获取一些其他 - 更有序 - 的方式来通知控件其底层数据源已更改/应该更改。
Your text boxes hold a reference to the first instance of the object you've created.
Now you're creating a second instance, supposedly in order to replace the existing instance, but the text boxes are unaware of the change.
You needs to either:
Pass the new instance to the text boxes (directly, as you assigned the first instance, or indirectly by having some "Model" object that both boxes are bound to).
Update the existing instance instead of replacing it with a new one (you can simply assign values to the fields, or create some "AssignFrom (other)" method, etc.)
Get yourself some other - more orderly - way of notifiying the controls that their underlying data source has changed / should be changed.