当整个对象发生变化时保持双向绑定

发布于 2024-09-15 21:33:00 字数 1391 浏览 6 评论 0原文

我有一个类:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苦笑流年记忆 2024-09-22 21:33:00

您的文本框包含对您创建的对象的第一个实例的引用。
现在您正在创建第二个实例,据说是为了替换现有实例,但文本框不知道更改。

您需要:

  1. 将新实例传递到文本框(直接传递给第一个实例,或者通过两个框都绑定到的某个“模型”对象间接传递)。

  2. 更新现有实例而不是用新实例替换它(您可以简单地为字段分配值,或创建一些“AssignFrom(其他)”方法等)

  3. 为自己获取一些其他 - 更有序 - 的方式来通知控件其底层数据源已更改/应该更改。

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:

  1. 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).

  2. 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.)

  3. Get yourself some other - more orderly - way of notifiying the controls that their underlying data source has changed / should be changed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文