Dot Net For All

INotifyPropertyChanged without property name

PropertyChanged Event using INotifyPropertyChanged

In WPF whenever we bind our view to the view-model class, there is always a need to propagate the changes in the view model to the UI. This is usually done by implementing the INotifyPropertyChanged interface to the view model class and subscribing to the raising the PropertyChanged event if any of the property is changed in the VM class.

The usual way to call the PropertyChanged event would be to call the function containing the event with the property name parameter, which can be erroneous some due to error in the spelling of the property which could lead to huge debugging time to find the source of the problem.

This can be avoided by using the [CallerMemberName] attribute along with the parameter as shown below.

        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        } 

Now in this case there is no need to call the OnPropertyChanged method every time with the property name specified as argument.
I have created a small demo application to show its working, the UI of the application looks like as shown below.

The code behind of this application is as shown below

 

DataContextClass dcClass = null;
        public MainWindow()
        {
            InitializeComponent();
            dcClass = new DataContextClass() { FirstName = "Vikram", LastName = "Chaudhary" };
            this.DataContext = dcClass;
        }

        private void ChangeName_Click(object sender, RoutedEventArgs e)
        {
            dcClass.FirstName = "Vikie";
        }

And the view model class of this project is as shown below

public class DataContextClass:INotifyPropertyChanged
    {
        private string firstName;
        private string lastName;

        public string FirstName
        {
            get
            {
                return firstName;
            }
            set
            {
                firstName = value;
                OnPropertyChanged();
            }
        }

        public string LastName
        {
            get { return lastName; }
            set
            {
                lastName = value;
                OnPropertyChanged();
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

As we can see from the above code there is no explicit name mentioned while calling the OnPropertyChanged method in any of the properties.
Now when you click the Button in which the FirstName of the class is being changed we should be able to see the change in the UI.

 

Top career enhancing courses you can't miss

My Learning Resource

Excel your system design interview