Skip to main content

WPF INotifyPropertyChanged Interface


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 datacontext(a class whose object is bound as datacontext with the parent of textbox or datacontext property of textbox) and if value text of textbox changes the value of string of datacontext 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 string object, you will notice that the value of text of textbox doesn't get changes. Because when source changes, no event fires from target (like if it is textbox then lost focus or property change event) and UpdateSourceTrigger does not make a pull.Hence both source and target are out of sync.

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

Fore Example:-

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

in the above example has a simple “Counter” class which has a “counter” property and this property is incremented by “Increment” method.
Now if we bind 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. MVVM Command
  2. WPF Binding
  3. WPF Data Validation


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



Popular posts from this blog

Generate QR Code in WPF

Introduction In my previous two blogs, we have discussed how to display generate and display various barcodes on the web page. In this blog, we are going to demonstrate how to Generate QR Code in the WPF application. Getting Started Here in the demonstration Generate QR Code in WPF , will generate QR code using third party library as there is not inbuilt library provided by Microsoft to generate QR code and will display in WPF image control, In the below, we will see the steps to display QR Code. As I mentioned in the above paragraph that there is no inbuild library provided by Microsoft to generate QR code, I have taken the help of the ZXing library which is a third party free library and available Nuget. This library provides various options to generate barcodes and QR Code from the user-friendly text. Generate QR Code in WPF Here are the steps to generate QR code and let's follow the steps to complete demonstrations. Demonstration:- Generate QR Code Open visual stud

WPF Binding

Introduction This blog describes WPF Binding and the measure elements exist with binding with XAML example in XAML and code-behind. Getting Started Binding helps in WPF to flow data from one object to another object, the object which fetches data is called source and the object which receives the data is called target. The Object a be a UI control or object of a class that means in binding you can bind a property of a class and property of another control to WPF UI controls as well. XAML Example <TextBox x:Name="sourceText" Grid.Row="0" /> <Button Grid.Row="1" Content="{Binding ElementName=sourceText, Path=Text}"/> Code Example Button btn=new Button(); Binding binding = new Binding("Text"); binding.Source = sourceText; btn.SetBinding(Button.ContentProperty, binding); In WPF binding has some measure properties or elements that we are using while developing application or projects, here we are going to d

Creating Application in Prism

Introduction This article explains an illustration of creating a windows application in Prism Library(WPF Prism). The solution includes recommended practices and techniques and is the basis for the procedures in Prism. This illustration created in the Visual Studio 2012, It can also developed in the visual studio 2008 and 2010, because wpf supports from .net framework 3.5 to latest version. Microsoft.Practices.Prism.dll. This assembly contains the implementation of the Prism Library core components such as modularity, logging services, communication services, and definitions for several core interfaces. It also contains the implementation of Prism Library components that target WPF applications, including commands, regions, and events. Microsoft.Practices.Prism.UnityExtensions.dll. This assembly contains base and utility classes you can reuse in applications built with the Prism Library that consume the Unity Application Block. For example, it contains a bootstrapper base class