Better Dependency Properties in WPF

Bindings are an essential part of the WPF Model-View-ViewModel design pattern.
They allow to react to changes in the view or model without any glue code.

Also as a Mac developer using Cocoa Bindings, I usually feel right at home with WPF bindings.
It allows very similar patterns when porting an app to Windows.

However, bindings in WPF require a Dependency Property - that is a special object that will take care of the update and notification mechanisms for your properties.

With Apple technologies, this is handled by KVO, which is basically part of the runtime.
As an example, in Swift, the only requirement for a KVO property is dynamic dispatch, meaning the following declaration can be used with bindings:

import Foundation

class Foo: NSObject
{
    @objc public dynamic var title: String?
}

Compared to a regular property, we only added @objc and dynamic.

In WPF, this is unfortunately not as easy.
As mentionned previously, bindings in WPF require a Dependency Property. This object has to be created (or registered) in order for bindings to work.

This leads to the following code:

using System.Windows;

class Foo
{
    public static readonly DependencyProperty TitleProperty = DependencyProperty.Register
    (
        "Title",
        typeof( string ),
        typeof( MyClass ),
        new PropertyMetadata( null )
    );

    public string Title
    {
        get => this.GetValue( TitleProperty ) as string;
        set => this.SetValue( TitleProperty, value );
    }
}

First, as a static member, we register the Dependency Property, specifying its name, type, owner type and other metadata such as default value.
Then we need to declare the property accessors, that will redirect to the dependency property itself.

And obviously, the same code has to be repeated for every property we want to use with bindings.
It leads to a lot of boilerplate code, and it's very easy to introduce bugs when copy-pasting such code, such as a typo in the property name or a wrong type or owner type.
Such bugs will usually result in an exception being thrown.

At DigiDNA, we developped a tiny module allowing a more compact declaration for Dependency Properties:

using System.Windows;
using BetterDP;

class Foo
{
    static Foo()
    {
        DP.InitializeProperties( typeof( Foo ) );
    }

    [DP]
    public string Title
    {
        get => this.Get< string >();
        set => this.Set( value );
    }
}

The only requirement is a call to DP.InitializeProperties in the class' static constructor.
Then, all properties may be decorated with the DP attribute to turn them into dependency properties, along with calling the generic Get and Set methods in the accessors.

Default values may be set directly from the DP attribute:

[DP( DefaultValue = 42 )]
public int Count
{
    get => this.Get< int >();
    set => this.Set( value );
}

You may also be notified about a property change by declaring a DependencyPropertyDidChange method in your class, such as:

protected void DependencyPropertyDidChange( string name, object value )
{}

This library is freely available on GitHub. It is also available as a NuGet package.

Enjoy : )