0

I have a custom Class that implements INotifyPropertyChanged as follows (extraneous and repeated properties removed):

 Public Class Map
        Implements INotifyPropertyChanged

        Private _wages As Decimal
        Property Wages As Decimal
            Get
                Return _wages
            End Get
            Set(value As Decimal)
                Debug.Print("Event Raised: " & _wages & " " & value)
                _wages = value
                RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(Wages))
            End Set
        End Property

Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

The Class is being Serialized/Deserialized correctly. When I load the XML file the TextBox that displays wages is not updating to reflect the Wages value.

I have the (Devexpress) TextBox DataBindings set with:

txtWages.DataBindings.Add(New Binding("EditValue", mymap, "Wages", False, DataSourceUpdateMode.OnPropertyChanged))

When I load the file I can see the old and new value from Debug.Print:

Event Raised: 0 13

However, txtWages stays as 0

I have read these answers Here and Here and tried to look for ideas but I am out of ideas.

EDIT: It seems that if I populate the class with the Deserialized XML and then set the Bindings it works, but not when the bindings are set first. I would have thought it would work either way.

Dave B
  • 659
  • 8
  • 29

2 Answers2

2

U have Error in code

PropertyChangedEventArgs need String (Name of property)

so "Wages"

 RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("Wages"))
  • I noticed that myself too. Even with passing the property name rather than the property value it won't update unless I set the binding after the class has been populated. – Dave B Oct 10 '17 at 14:35
  • so use Property Set for Map object and when u populate new object u can easy rebind form control – Norbert Ziemniak Oct 10 '17 at 16:10
1
  1. You should provide name of the property to the PropertyChangedEventArgs

    RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(Nameof(Wages))
    
  2. You did not show, but obviously when you "load" file and deserialize class you create new instance of Map and set it to mymap. Where control's binding will still refer to the original instance and listen it's events.

Create "wrapper" viewmodel with property of type Map and raise property change event when you load file.

Public Class ViewModel
    Private _Map As Map
    Property Map As Map
        Get
            Return _Map
        End Get
        Set(value As Map)
            _Map = value
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(Nameof(Wages))
        End Set
    End Property  
End Class

Then in the form do binding

Dim viewmodel = new ViewModel()
viewmodel.Map = DeserializeMap() ' Your deserialize logic
txtWages.DataBindings.Add("EditValue", 
                          viewmodel, 
                          "Map.Wages", 
                          False, 
                          DataSourceUpdateMode.OnPropertyChanged)
Fabio
  • 31,528
  • 4
  • 33
  • 72
  • `MyMap` is declared before the `New` sub. The code to Deserialize is: `MyMap = BM.Utilities.FileHelper.Deserialize(Of Map)("Routes.xml")` surely thats the same instance? – Dave B Oct 10 '17 at 15:12
  • 1
    Deserialization will create new instance of type `Map`. – Fabio Oct 10 '17 at 15:20