WPF Notepad Application [Part 2]

In this episode of creating a WPF Notepad Application, we set up our project folders, create our model objects, and set up a unique implementation of INotifyPropertyChanged. With this new implementation we are going to have our OberservableObject handle assigning values to the supplied fields, as well as notifying a property change without the use of magic strings. We will accomplish this by creating a generic method for property changes and by using the CallerMemberName attribute.

 

YouTube

 

Code from this episode

ObservableObject.cs

    public class ObservableObject : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged<T>(ref T property, T value, [CallerMemberName] string propertyName = "")
        {
            property = value;
            var handler = PropertyChanged;
            if(handler != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *