How To Use CallerMemberName Attribute In WPF With MVVM Pattern

I really like XAML and Data binding. I always follow the "No Code Behind" approach. Data binding is a simple way for applications to present and interact with data. Data binding allows the flow of data between UI and data objects on user interface. When a binding is established with INotifyPropertyChanged and the data model is changed, it reflects to update automatically to the UI elements.

With the release of Microsoft Visual Studio 2012 and .NET 4.5, we have got a new attribute “CallerMemberName”.

With this new feature, you can write INotifyPropertyChanged (Interface) code without having to worry about renaming properties and string parameters.

In order to use this attribute, we first need to add a using statement to System .Runtime.CopilerServices

  1. using System.Runtime.CompilerServices;   

Here is a typical implementation of INotifyPropertyChanged “ViewModelBase.cs

  1. public event PropertyChangedEventHandler Property Changed;  
  2.   
  3. protected void IPropertyChanged(string Propertname) {  
  4.  PropertyChangedEventHandler Handler = this.PropertyChanged;  
  5.  if (Handler != null)  
  6.   Handler(thisnew PropertyChangedEventArgs(Propertname));  
  7. }   
Here, we have PropertyChanged event. This is an event that is specified by the interface.
  1. public abstract class ViewModelBase:INotifyPropertyChanged    

The INotifyPropertyChanged Interface is included in System.ComponentModel namespace.

  1. using System.ComponentModel;     
Here is a typical implementation of [CallerMemberName]ViewModelBase.cs”.
  1. protected bool SetProperty < T > (ref T Storage, T Value, [CallerMemberName] string Propertname = null) {  
  2.  if (EqualityComparer < T > .Default.Equals(Storage, Value)) return false;  
  3.  Storage = Value;  
  4.  IPropertyChanged(Propertname);  
  5.  return true;  
  6. }   

Here is an implementation of model class, and we called the IPropertyChanged event in the Setter of model properties

  1. public class Person: ViewModelBase {  
  2.  private string _FirstName;  
  3.   
  4.  public string FirstName {  
  5.   get {  
  6.    return _FirstName;  
  7.   }  
  8.   set {  
  9.   
  10.    SetProperty(ref this._FirstName, value);  
  11.   }  
  12.  }  
  13.  private string _LastName;  
  14.   
  15.  public string LastName {  
  16.   get {  
  17.    return _LastName;  
  18.   }  
  19.   set {  
  20.   
  21.    SetProperty(ref this._LastName, value);  
  22.   
  23.   }  
  24.  }  
  25. }   

Here is an implementation of model class in ViewModel.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using BooleanConverterExample.Model;  
  7.   
  8. namespace BooleanConverterExample.ViewModel {  
  9.  public class MainWindowViewModel {  
  10.   
  11.   public MainWindowViewModel() {  
  12.    startup();  
  13.   }  
  14.   
  15.   public void startup() {  
  16.    Model.Person P1 = new Model.Person();  
  17.    P1.FirstName = "Karthikeyan";  
  18.    P1.LastName = "Karuppusamy";  
  19.   }  
  20.  }  
  21. }   

So, our implementations is done. We will add some breakpoints in ViewModelBase class to test PropertyChanged event.

Output

Here, you can see the difference between object old value and new value.

[SetProperty Calling]

[SetProperty Calling]

[SetProperty Calling]

 

I hope you liked this blog. Happy Coding.

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now