分层 INotifyPropertyChanged

发布于 2024-07-24 04:41:12 字数 306 浏览 3 评论 0原文

我有一个为属性实现 INotifyPropertyChanged 的​​类。

我有一个绑定到该属性的控件。

我有另一个类监听 propertychanged 事件。 在该类的事件处理程序中,我更改了代码中属性的值。

我遇到的问题是,我不想在事件处理程序中执行任何逻辑,以便下次由于代码导致属性更改而触发事件处理程序。

但是,如果用户同时更改属性的值(通过异步 gui 输入),我仍然希望触发逻辑。 我还需要确保控件得到更新(这是双向绑定)。

在不让事情变得一团糟的情况下,最好的方法是什么?

I have a class that implements INotifyPropertyChanged for a property.

I have a control that is bound to that property.

I have another class that listens to the propertychanged event. In the event handler of that class I change the value of the property in code.

The problem I have is that I don't want to do any logic in the event handler for the next time it will fire due to the change of the property due to code.

However if the user changes the value of the property in the mean time (via async gui input) I still want the logic to fire. I also need to make sure that the control gets updated (this is twoway binding).

What is the best way to do this without this becoming a complete mess?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

那小子欠揍 2024-07-31 04:41:12

实现此目的的一种方法是重构属性上的 setter,以便它调用一个方法,该方法采用一个指示是否引发事件的参数。 这是一个简单的代码示例:

Imports System.ComponentModel

Public Class Class1
    Implements INotifyPropertyChanged

    Public Property MyData() As String
        Get
            Return _myData
        End Get
        Set(ByVal value As String)
            SetMyData(value, True)
        End Set
    End Property
    Private Sub SetMyData(ByVal value As String, ByVal triggerPropertyChanged As Boolean)
        _myData = value
        If triggerPropertyChanged Then
            OnPropertyChanged("MyData")
        End If
    End Sub
    Private _myData As String

    Private Sub OnPropertyChanged(ByVal propertyName As String)
        SetMyData("new value", False)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
    End Sub
    Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
End Class

One way to accomplish this would be to refactor the setter on your property so that it called a method taking a parameter indicating whether or not to raise the event. Here is a simple code sample:

Imports System.ComponentModel

Public Class Class1
    Implements INotifyPropertyChanged

    Public Property MyData() As String
        Get
            Return _myData
        End Get
        Set(ByVal value As String)
            SetMyData(value, True)
        End Set
    End Property
    Private Sub SetMyData(ByVal value As String, ByVal triggerPropertyChanged As Boolean)
        _myData = value
        If triggerPropertyChanged Then
            OnPropertyChanged("MyData")
        End If
    End Sub
    Private _myData As String

    Private Sub OnPropertyChanged(ByVal propertyName As String)
        SetMyData("new value", False)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
    End Sub
    Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
End Class
绾颜 2024-07-31 04:41:12

您的问题有点模糊,但您可以检查该值是否确实发生了变化,以确定是否应该执行您的逻辑。
如果您包含一些代码以更具体地表达您的问题,那就更好了。

Your question is a bit vague, but you can check to see if the value has actually changed to determine if you should do your logic.
It would be better if you include some code to be more specific in your question.

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