Home > Enterprise >  PropertyChanged subroutine is not called/triggered
PropertyChanged subroutine is not called/triggered

Time:10-29

Windows Forms Application in VS2012

Target .NET Framework: 4.5

Created a project containing a Form (Form1) and a class (Class1). Form has a button (Button1) object. Code below:

Class1:

Imports System.ComponentModel

Public Class Class1
    Implements INotifyPropertyChanged

    Private _PropValue As String
    Public Property PropValue As String
        Get
            Return _PropValue
        End Get
        Set(value As String)
            _PropValue = value
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("PropValue"))
            MsgBox("Event Raised") ' FOR TESTING
        End Set
    End Property

    Public Event PropertyChanged(sender As Object, e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged
End Class

Form1:

Imports System.ComponentModel

Public Class Form1

    Private WithEvents _Class1 As Class1

    Private Sub _Class1_PropertyChanged(sender As Object, e As PropertyChangedEventArgs) Handles _Class1.PropertyChanged
        MsgBox("Property Changed subroutine") ' FOR TESTING
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim tmpObj As Class1 = New Class1

        tmpObj.PropValue = "Value-1"

        tmpObj.PropValue = "Value-2"
    End Sub
End Class

When executing the application and clicking on Button1, received 2 popup messages "Event Raised" from MsgBox in Class1. I'm trying to get the message "Property Changed subroutine" from _Class1_PropertyChanged subroutine. Haven't been successful so far.

CodePudding user response:

You're not changing the property value of the object whose event you're handling. You're handling the event of the object you assign to the _Class1 field but you're changing the property value of a different object assigned to the tmpObj local variable. Get rid of that local variable and use the field only.

  • Related