BetterDP 1.0.4

dotnet add package BetterDP --version 1.0.4                
NuGet\Install-Package BetterDP -Version 1.0.4                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="BetterDP" Version="1.0.4" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add BetterDP --version 1.0.4                
#r "nuget: BetterDP, 1.0.4"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install BetterDP as a Cake Addin
#addin nuget:?package=BetterDP&version=1.0.4

// Install BetterDP as a Cake Tool
#tool nuget:?package=BetterDP&version=1.0.4                

BetterDP

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
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net5.0-windows7.0 is compatible.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.1 is compatible. 
.NET Framework net462 is compatible.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETCoreApp 3.1

    • No dependencies.
  • .NETFramework 4.6.2

    • No dependencies.
  • net5.0-windows7.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.4 657 7/3/2021
1.0.3 420 6/24/2021
1.0.2 436 4/27/2021
1.0.1 673 7/29/2020
1.0.0 566 7/28/2020