Igor Kulman

INotifyPropertyChanged the easy way in Windows Phone and Windows 8

· Igor Kulman

If you develop Windows Phone, Windows 8, Silverlight or WPF apps using the MVVM pattern, you are familiar with the INotifyPropertyChanged interface.

Typical implementation

In a typical implementation, you usually have a base class implementing the interface, like

public class BaseViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public void NotifyPropertyChanged(string propertyname)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyname));
        }
    }     
}

or use a framework like MVVMLight, Prism or Caliburn Micro that provides such base class for you. In your view models you have properties using the PropertyChanged method

public class UserViewModel: BaseViewModel
{
    public string FirstName
    {
        get { return _firstName; }
        set
        {
            if (_firstName != value)
            {
                _firstName = value;
                NotifyPropertyChanged("FirstName");
                NotifyPropertyChanged("FullName");
            }
        }
    }
    private string _firstName;

    public string Surname
    {
        get { return _surname; }
        set
        {
            if (_surname != value)
            {
                _surname = value;
                NotifyPropertyChanged("Surname");
                NotifyPropertyChanged("FullName");
            }
        }
    }
    private string _surname;

    public string FullName
    {
        get { return String.Format("{0} {1}", FirstName, Surname); }
    }
}

You do not need to create such properties by hand, you can use a snippet, but it is still a lot of code to do such a simple thing.

Meet Fody

Fody is an assembly weaver for .NET that plugs to the build process of your project and modifies the IL of your assemblies according to your needs. It supports .net 3.5, .net 4, .net 4.5, Silverlight 4, Silverlight 5, Windows Phone 7, Windows Phone 8, Metro on Windows 8, Mono, MonoTouch, MonoDroid and PCL. No installation or configuration is required, you just need to install the Fody Nuget package.

INotifyPropertyChanged using Fody (80% of the time)

Fody has a PropertyChanged addin that does all the INotifyPropertyChanged plumbing for you. If you want the same behavior as in my typical implementation example, there is no need for a base class. Install the PropertyChanged.Fody Nuget package, decorate your view model class with the ImplementPropertyChanged attribute and use just basic properties

[ImplementPropertyChanged]
public class UserViewModel
{
    public string FirstName { get; set; }
    public string Surname { get; set; }

    public string FullName
    {
        get { return String.Format("{0} {1}", FirstName, Surname); }
    }                
}

That is it. Less code, the same behaviour. You can verify it with tools like Telerik JustDecompile.

The remaining 20%

Fody does all the plumbing for you. It even knows that the FullName property uses FirstName and Surname and raises the PropertyChanged event for it when any og the two properties changes. The PropertyChanged.Fody addin contains attributes, that you can use to define dependencies

[DependsOn("FirstName ","Surname")]
public string FullName { get; set; }

or to raise the PropertyChanged event for any other property

[AlsoNotifyFor("FullName")]
public string FirstName { get; set; }

Sometimes you want to execute some additional code in the setter. Fody allows it, just create a method with the name On_PropertyName_Changed

public void OnFirstNameChanged()
{
    Debug.WriteLine("FirstName Changed");
}

If you use a framework and you want to raise the PropertyChanged event through a method of this framework, it is not a problem. You just need to set the EventInvokerNames options. The Fody documentation even describes what to set for some of the frameworks so you do not have to figure it out for yourself.

See also