Showing posts with label PropertyChangedEventHandler. Show all posts
Showing posts with label PropertyChangedEventHandler. Show all posts

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.