Use Of CallerMemberName With INotifyPropertyChanged Interface In WPF MVVM

CallerMemberName is a new supper useful attribute released with Microsoft Dot Net Framework version 4.5. By using this attribute, there is no need to worry about passing property names of string parameter while notifying Property change.

CallerMemberName is a member of System.Runtime.CopilerServices namespace.

To know more about INotifyPropertyChanged, and how to use it, please refer to my detailed post by clicking the below-mentioned link.

Use INotifyPropertyChanged Interface In WPF MVVM

Before the release of this attribute, default implementation for INotifyPropertyChanged Interface was as below.

  1. public event PropertyChangedEventHandler PropertyChanged;  
  2. public void RaisePropertyChange(string propertyname) {  
  3.     if (PropertyChanged != null) {  
  4.         PropertyChanged(thisnew PropertyChangedEventArgs(propertyname));  
  5.     }  
  6. }  

To notify that on property value has changed, we have to call RaisePropertyChange with property name as string parameter.

Check the below model class implementation.

  1. public class User: NotifyPropertyChanged {  
  2.     private string _fisrtname;  
  3.     public string FirstName {  
  4.         get {  
  5.             return _fisrtname;  
  6.         }  
  7.         set {  
  8.             _fisrtname = value;  
  9.             RaisePropertyChange("FirstName");  
  10.         }  
  11.     }  
  12.     private string _lastname;  
  13.     public string LastName {  
  14.         get {  
  15.             return _lastname;  
  16.         }  
  17.         set {  
  18.             _lastname = value;  
  19.             RaisePropertyChange("LastName");  
  20.         }  
  21.     }  

As you can see here, we have called RaisePropertyChange method by passing property name as string parameter to raise property change notification.

After CallerMemberName attribute, implementation of INotifyPropertyChanged Interface looks like below.

  1. public event PropertyChangedEventHandler PropertyChanged;  
  2. public void RaisePropertyChange([CallerMemberName] string propertyname = null) {  
  3.     if (PropertyChanged != null) {  
  4.         PropertyChanged(thisnew PropertyChangedEventArgs(propertyname));  
  5.     }  
  6. }  

Here, we have added CallerMemberName attribute with RaisePropertyChange Method paramenter. Please note that this attribute can be used only with parameters having default value.

Now, after new implementation of the interface, Model Class implementation will be as below.

  1. private string _fisrtname;  
  2. public string FirstName {  
  3.     get {  
  4.         return _fisrtname;  
  5.     }  
  6.     set {  
  7.         _fisrtname = value;  
  8.         RaisePropertyChange();  
  9.     }  
  10. }  
  11. private string _lastname;  
  12. public string LastName {  
  13.     get {  
  14.         return _lastname;  
  15.     }  
  16.     set {  
  17.         _lastname = value;  
  18.         RaisePropertyChange();  
  19.     }  
  20. }  

As you can see, now we have to call only RaisePropertyChange method without passing string parameter and property name will fetch automatically.

If you want to raise property changes for any other property then you can call RaisePropertyChange method by passing that property name as a string parameter.