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.
- public event PropertyChangedEventHandler PropertyChanged;
- public void RaisePropertyChange(string propertyname) {
- if (PropertyChanged != null) {
- PropertyChanged(this, new PropertyChangedEventArgs(propertyname));
- }
- }
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.
- public class User: NotifyPropertyChanged {
- private string _fisrtname;
- public string FirstName {
- get {
- return _fisrtname;
- }
- set {
- _fisrtname = value;
- RaisePropertyChange("FirstName");
- }
- }
- private string _lastname;
- public string LastName {
- get {
- return _lastname;
- }
- set {
- _lastname = value;
- RaisePropertyChange("LastName");
- }
- }
- }

Rafnas T PPosted Mar 24, 2018, 2:53 AM
Nice share, keep sharing..