How to: Implement Property Change Notification
To support OneWay or TwoWay binding to enable your binding target properties to automatically reflect the dynamic changes of the binding source (for example, to have the preview pane updated automatically when the user edits a form), your class needs to provide the proper property changed notifications. This example shows how to create a class that implements INotifyPropertyChanged.
To implement INotifyPropertyChanged you need to declare the PropertyChanged event and create the OnPropertyChanged method. Then for each property you want change notifications for, you call OnPropertyChanged whenever the property is updated.
Imports System.ComponentModel ' This class implements INotifyPropertyChanged ' to support one-way and two-way bindings ' (such that the UI element updates when the source ' has been changed dynamically) Public Class Person Implements INotifyPropertyChanged Private personName As String Sub New() End Sub Sub New(ByVal Name As String) Me.personName = Name End Sub ' Declare the event Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged Public Property Name() As String Get Return personName End Get Set(ByVal value As String) personName = value ' Call OnPropertyChanged whenever the property is updated OnPropertyChanged("Name") End Set End Property ' Create the OnPropertyChanged method to raise the event Protected Sub OnPropertyChanged(ByVal name As String) RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(name)) End Sub End Class
To see an example of how the
Person class can be used to support TwoWay binding, see How to: Control When the TextBox Text Updates the Source.
For the complete sample, see Simple Binding Sample.