Skip to content

DigiDNA/BetterDP

main
Switch branches/tags
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

BetterDP

Build Status Issues Status License
Contact

Simpler declaration for WPF Dependency Properties

About

Dependency Properties are a neat feature of WPF, allowing powerful data binding mechanisms.

However, declaring custom properties is not really straightforward and leads to a lot of boilerplate code.
As an example:

using System.Windows;

public class Foo: DependencyObject
{
    public static readonly DependencyProperty BarProperty = DependencyProperty.Register
    (
        "Bar",
        typeof( string ),
        typeof( Foo ),
        new PropertyMetadata()
    );

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

This package provides a way to reduce the amount of code needed to declare such a property, by introducing a custom [DP] attribute.
The new syntax is then:

using System.Windows;
using BetterDP;

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

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

Initialization

BetterDP will automatically create dependency properties for every property marked with the DP attribute.
However, this unfortunately cannot be done lazily, as dependency properties usually need to be available before constructing an object, especially when using XAML bindings.

This is why the example above uses a static constructor.
Calling DP.InitializeProperties will ensure every dependency property is properly initialized.

Default values

A default value can be provided directly within the [DP] attribute, such as:

[DP( DefaultValue = "hello, world" )]
public string Bar
{
    get => this.Get< string >();
    set => this.Set( value );
}

This is the equivalent of setting a default value with PropertyMetadata or FrameworkPropertyMetadata.

Change Handlers

In order to be notified when a property has changed, you may implement the following method in your class:

protected virtual void DidChangeDependencyProperty( string name, object value )
{}

It will automatically be called when a property marked with [DP] has changed.

License

BetterDP is released under the terms of the MIT License.

Repository Infos

Owner:          DigiDNA
Web:            www.digidna.net
Blog:           imazing.com/blog
Twitter:        @DigiDNA
GitHub:         github.com/DigiDNA

About

Simpler WPF Dependency Properties

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages