How to implement INotifyPropertyChanged interface in C# and WPF


 
To notify a target element in WPF that the source property value has been changed , The containing class will have to implement the INotifyPropertyChanged Interface 


Below is a sample abstract class that implements this interface, All you have to do is extend this abstract class in your ViewModel class or any class where your properties are declared.


 using System;
 using System.ComponentModel;

 namespace Logiphix
 {
    abstract public class PropogatePropertyChangesToTarget :  INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public void NotifyPropertyChanged(string info)
        {
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
        }
    }
}


How can you use it?

Lets declare a sample class and extend the above created abstract class

public class SampleClass PropogatePropertyChangesToTarget 
{
      /* I have my properties declared here*/
      private string employeeName;

      public string EmployeeName 
      {
          get
             { 
                   return employeeName;
             }

          set
             {
                   employeeName = value;

                   // Call the method here in the setter
                   NotifyPropertyChanged("EmployeeName"); 
             }
      }
}

Now if any target property is bound to this above property, It is likely to get notified that its value has changed.





3 comments:

  1. Thanks , this helped

    ReplyDelete
  2. Slingo Games - Titanium Bong
    If you're looking titanium forging for a Slingo game that's 제이티엠허브출장안마 as easy to titanium apple watch band understand as Slingo and as exciting as nier titanium alloy Slingo, then you've come to 2020 ford ecosport titanium the right place.

    ReplyDelete