Windows Presentation Foundation
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.

Example

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.

Visual Basic
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
C#
using System.ComponentModel;

namespace SDKSample
{
  // 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 : INotifyPropertyChanged
  {
      private string name;
      // Declare the event
      public event PropertyChangedEventHandler PropertyChanged;

      public Person()
      {
      }

      public Person(string value)
      {
          this.name = value;
      }

      public string PersonName
      {
          get { return name; }
          set
          {
              name = value;
              // Call OnPropertyChanged whenever the property is updated
              OnPropertyChanged("PersonName");
          }
      }

      // Create the OnPropertyChanged method to raise the event
      protected void OnPropertyChanged(string name)
      {
          PropertyChangedEventHandler handler = PropertyChanged;
          if (handler != null)
          {
              handler(this, new PropertyChangedEventArgs(name));
          }
      }
  }
}

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.

See Also

Concepts

Other Resources

Tags : inotify


Community Content

jkallay
Clarification needed on WPF property name resolution
Some clarification is needed on how WPF responds to property change events based on the property name passed to the PropertyChanged event. Specifically, how are explicit interface property implementations (C#) handled? It is possible to have two different bindings to two different properties on the same object where both properties have the same name, e.g.
<Binding Path=(MyInterface.MyProperty)/> and <Binding Path=MyInstance.MyProperty/>. Which of these bindings update on MyInstance.PropertyChanged(MyInstance,"MyProperty")? Does the explicit interface implementation have to fully qualify the property name, i.e. MyInstance.PropertyChanged(MyInstance,"MyInterface.MyProperty") in order for that binding to update, or do both bindings get updated?
Tags :

Page view tracker