Click here to Skip to main content
15,896,063 members
Articles / Desktop Programming / XAML
Tip/Trick

Property Change Notification with Compile Time Safety

Rate me:
Please Sign up or sign in to vote.
3.79/5 (5 votes)
9 Dec 2014CPOL 32.6K   7   27
Simple trick to provide compile time safety for client side classes using INotify to notify when property changes (used generally by XAML bindings)

Introduction

Here is a cool little trick to ensure compile time safety when raising property change notification events. Instead of using strings as in "PropertyName", we can very well use a lamda expression like () => PropertyName.

Background

Property change notification is generally used in applications where XAML binding is used and hence these events are using to ensure two way binding with the UI as in MVVM design pattern.

Using the Code

Below is a typical implementation of INotifyPropertyChanged interface which provides no compile time safety and client object passes string to notify of a change in the property:

C#
public class SampleClass : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    
    private string _name;
    
    public string Name
    {
        get { return _name; }
        set
        {
            if (_name == value) return;
            _name = value;
            OnPropertyChanged("Name");
        }
    }
    
    protected virtual void OnPropertyChanged(string propertyName)
    {
        if (propertyName != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

So, in order to ensure compile time safety, we provide a simple overload for OnPropertyChanged method as below, in turn using a helper method, which can very well be hosted in its own helper class. For the purpose of this example, I have it below:

C#
protected virtual void OnPropertyChanged<T>(Expression<Func<T>> propertyExpression)
{
    OnPropertyChanged(ExtractPropertyName(propertyExpression));
}

private string ExtractPropertyName<T>(Expression<Func<T>> propertyExpression)
{
    return (((MemberExpression)(propertyExpression.Body)).Member).Name;
}

And now, the client properties can be changed to raise the events like this:

C#
public string Name
{
    get { return _name; }
    set
    {
        if (_name == value) return;
        _name = value;
        OnPropertyChanged(() => Name);
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: [My vote of 2] CallerMemberName Pin
TSFolkes17-Dec-14 19:20
TSFolkes17-Dec-14 19:20 
GeneralMy vote of 4 Pin
Duncan Edwards Jones9-Dec-14 22:59
professionalDuncan Edwards Jones9-Dec-14 22:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.