views:

93

answers:

2

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?

A: 

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.

Michael S. Scherotter
+1  A: 

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
Guster_Q