Skip to content

.NET 4.5 has Streamlined Utilizing INotifyPropertyChanged

December 17, 2012

The new CallerMemberName attribute in .NET 4.5 makes using INotifyPropertyChanged much nicer, plus greatly reduces the potential for creating hard to find bugs.  I really like this small simplification that has a big payback.

Now you can implement a property with PropertyChanged notification WITHOUT having to pass a string containing the property name that is changing to the method raising the PropertyChanged event.  This was often a source of hard to debug errors, and previous workarounds involving lambda expressions were sometimes slow and always more awkward to type and read. 

Here’s what the new implementation of a .NET property looks like using INotifyProperty changed implemented with the CallerMemberName attribute as the optional argument in its RaisePropertyChanged() method.  Note that the below call to RaisePropertyChanged() does NOT have the name of the property of “InkColor” as its argument as required in pre .NET 4.5 code:

    public class SomeViewModel : ViewModelBase
    {
        public Color InkColor
        {
            get { return m_InkColor; }
            set
            {
                if (value != m_InkColor)
                {
                    m_InkColor = value;
                    RaisePropertyChanged();
                }
            }
        }
        private Color m_InkColor;
    }

The remainder of this story is told by the implementation of INotifyPropertyChanged, below in the ViewModelBase class.  This shows how the CallerMemberName fits into the picture, as the optional parameter to the RaisePropertyChanged() method.  When the argument, propertyName, is empty the name of the calling property is placed in the propertyName variable, otherwise the value in propertyName remains as is.  In other words, this is compatible with previous implementations allowing you to specify a property name string as required in days past.

    public abstract class ViewModelBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChanged([CallerMemberName] String propertyName = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

Your class implementing INotifyPropertyChanged must have the following using statement to include the namespace containing the CallerMemberName attribute.

using System.Runtime.CompilerServices;

I hope you find this as useful as I do!

George Stevens

Creative Commons License dotnetsilverlightprism blog by George Stevens is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Based on a work at dotnetsilverlightprism.wordpress.com.

Advertisements
Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: