INotifyPropertyChanged and Auto-Properties

Viewed 26523

Is there a way to use INotifyPropertyChanged with auto-properties? Maybe an attribute or something other, not obvious to me.

public string Demo{
    get;set;
}

For me, auto-properties would be an extremely practicall thing, but almost always, I have to raise the PropertyChanged-event if the property value has been changed and without a mechanism to do this, auto-properties are useless for me.

6 Answers

I tried other ways first using:

Those methods work, however I've still had to muddy the accessor methods of all properties in the Model or use a dynamic object which kills your auto-completion when coding. It's also worth mentioning that WinForms don't support binding to dynamic objects unfortunately.

Solution

Eventually I came across ReactiveUI.Fody. It's a simple to use solution using Fody and ReactiveUI. I used this in my WPF project successfully.

It weaves in the required boilerplate code, RaisePropertyChanges and ObservableAsPropertyHelper, at compile time.

Project Dependencies

https://github.com/kswoll/ReactiveUI.Fody

I installed the following packages in my project with NuGet:

<packages>
  <package id="Fody" version="2.0.7" targetFramework="net452" developmentDependency="true" />
  <package id="reactiveui" version="7.4.0" targetFramework="net452" />
  <package id="ReactiveUI.Fody" version="2.2.11" targetFramework="net452" />
  <package id="reactiveui-core" version="7.4.0" targetFramework="net452" />
  <package id="Rx-Core" version="2.2.5" targetFramework="net452" />
  <package id="Rx-Interfaces" version="2.2.5" targetFramework="net452" />
  <package id="Rx-Linq" version="2.2.5" targetFramework="net452" />
  <package id="Rx-Main" version="2.2.5" targetFramework="net452" />
  <package id="Rx-PlatformServices" version="2.2.5" targetFramework="net452" />
  <package id="Rx-XAML" version="2.2.5" targetFramework="net452" />
  <package id="Splat" version="1.6.0" targetFramework="net452" />
</packages>

Fody Setup

You'll need to create a FodyWeavers.xml file in the top level of you project so it looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<Weavers>
  <ReactiveUI/>
</Weavers>

Usage

You just set your Model classes to inherit off ReactiveObject and add the [Reactive] attribute above any property you want to notify of changes.

using ReactiveUI;
using ReactiveUI.Fody.Helpers;

namespace name.domain.some{
    class SomeClass : ReactiveObject {
        [Reactive]
        public string SomeProperty { get; set; }
    }
}

Job done! Rebuild your project and check the output window in Visual Studio. You should see some lines outputted from Fody as it processes your annotated Model classes and injects the appropriate boilerplate code. It should look something like this:

enter image description here

Further Info

More useful info can be found here.

Useful answer by crea7or, ended up using it in my latest project. One way to improve it is by rewriting SetProperty this way:

protected bool SetProperty<T>(ref T storage, T value, string[] dependentPropertyNames = null, [CallerMemberName] string propertyName = null)
{
    if (Equals(storage, value))
    {
        return false;
    }

    storage = value;
    this.OnPropertyChanged(propertyName);

    if(dependentPropertyNames != null)
    {
        foreach(var dependentPropertyName in dependentPropertyNames)
        {
            OnPropertyChanged(dependentPropertyName);
        }
    }

    return true;
}

It now allows to pass in dependent property names allowing view to know that dependent property value has changed even though it was not set directly, but as a result of other properties being set.

Usage:

private string firstName;
public string FirstName
{
    get { return firstName; }
    set { SetProperty(ref firstName, value, new[] { "FullName" }); }
}

private string lastName;
public string LastName
{
    get { return lastName; }
    set { SetProperty(ref lastName, value, new[] { "FullName" }); }
}

public string FullName
{
    get { return $"{FirstName} {LastName}"; }
}
Related