WPF INotifyPropertyChanged Interface

Kailash Chandra Behera | Wednesday, August 10, 2016

Introduction

This article describes about INotifyPropertyChanged Interface and demonstrates how to use in WPF application using C Sharp(C#)

Getting Started

In WPF Data Binding data flows from one object to another object (Object to UI elements or vise versa). The object that emits data is called source and the object that receivs data is called Target.

When WPF UI element is bound with an object, for example when Textbox text property is bound with a string property of data context(a class whose object is bound as data context with the parent of the textbox or data context property of textbox) and if value text of textbox changes the value of a string of data context also changes due to UpdateSourceTrigger. Because UpdateSourceTrigger is used in WPF to update source in data binding, it makes a PULL to the source to get the latest data when lost focus or property change event happens on the target.

But if you make changes in a string object, you will notice that the value of text of textbox doesn't get changes. Because when source changes, no event fires from the target (like if it is textbox then lost focus or property change event) and UpdateSourceTrigger does not make the pull. Hence both source and target are out of sync.

To overcome this problem WPF introduces INotifyPropertyChanged Interface, which helps update or notify to target when any changes happen in the source.

For Example:-

 public class Counter : INotifyPropertyChanged  
  {           
   private int _counter=0;   
   public int counter       
   {  
        get { return _counter; }   
   }  
      public void Increment()  
      {  
        _counter++;  
        PropertyChanged(this, new PropertyChangedEventArgs("counter"));  
      }    
      public event PropertyChangedEventHandler PropertyChanged;  
   }   

the above example has a simple “Counter” class which has a “counter” property and this property is incremented by the “Increment” method.
Now if we bind the WPF label or textbox to the “counter” property and call the “Increment” method the new “Counter” value will not be propagated to the target. Because invoking a method does not trigger any “UpdateSourceTrigger” event.
So after calling the “Increment” method the “counter” value of the source and the target is out of synch.

So create a push event from the source you need to first implement “INotifyPropertyChanged” interface as shown in the below figure. Now when someone calls the “Increment” method you can raise an event saying that the “counter” property has changed by calling “PropertyChanged” function as shown in the below code.

 PropertyChanged(this, new PropertyChangedEventArgs("counter"));   

In simple words the source sends a notification to the target WPF object that data has changed in the source and he should refresh himself with the fresh data.

Related Articles

  1. WPF Dispatcher
  2. Command In MVVM
  3. WPF Data Validation: IDataErrorInfo
  4. Data Validation in WPF
  5. UpdateSourceTrigger
  6. WPF Triggers

Summary

In the above of this article we have seen how INotifyPropertyChanged Interface helps to notify targer or client when any changes happens in surce side. Hope this article may helpful to you

Thanks