DependencyPropertyGenerator 1.1.4

There is a newer version of this package available.
See the version list below for details.
dotnet add package DependencyPropertyGenerator --version 1.1.4                
NuGet\Install-Package DependencyPropertyGenerator -Version 1.1.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="DependencyPropertyGenerator" Version="1.1.4">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DependencyPropertyGenerator --version 1.1.4                
#r "nuget: DependencyPropertyGenerator, 1.1.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 DependencyPropertyGenerator as a Cake Addin
#addin nuget:?package=DependencyPropertyGenerator&version=1.1.4

// Install DependencyPropertyGenerator as a Cake Tool
#tool nuget:?package=DependencyPropertyGenerator&version=1.1.4                

DependencyPropertyGenerator

Dependency property and routed event source generator for WPF/UWP/WinUI/Uno/Avalonia/MAUI platforms.

Install



<PackageReference Include="DependencyPropertyGenerator" Version="1.0.0" PrivateAssets="all" ExcludeAssets="runtime" />

NOTE: When installing via NuGet just replace <IncludeAssets>runtime with <IncludeAssets>compile for it to work properly.

Usage

using DependencyPropertyGenerator;
using System.Windows.Controls;

#nullable enable

namespace H.Generators.IntegrationTests;

[DependencyProperty<bool>("IsSpinning", DefaultValue = true, Category = "Category", Description = "Description")]
public partial class MyControl : UserControl
{
    // Optional
    partial void OnIsSpinningChanged(bool oldValue, bool newValue)
    {
    }
}

[AttachedDependencyProperty<object, TreeView>("SelectedItem", DefaultBindingMode = DefaultBindingMode.TwoWay)]
public static partial class TreeViewExtensions
{
    // Optional
    static partial void OnSelectedItemChanged(TreeView sender, object? oldValue, object? newValue)
    {
    }
}

will generate:

//HintName: MyControl.Properties.IsSpinning.generated.cs

#nullable enable

namespace H.Generators.IntegrationTests
{
    public partial class MyControl
    {
        /// <summary>
        /// Description<br/>
        /// Default value: true
        /// </summary>
        public static readonly global::System.Windows.DependencyProperty IsSpinningProperty =
            global::System.Windows.DependencyProperty.Register(
                name: "IsSpinning",
                propertyType: typeof(bool),
                ownerType: typeof(global::H.Generators.IntegrationTests.MyControl),
                typeMetadata: new global::System.Windows.FrameworkPropertyMetadata(
                    defaultValue: (bool)true,
                    flags: global::System.Windows.FrameworkPropertyMetadataOptions.None,
                    propertyChangedCallback: static (sender, args) =>
                    {
                        ((global::H.Generators.IntegrationTests.MyControl)sender).OnIsSpinningChanged(
                            (bool)args.OldValue,
                            (bool)args.NewValue);
                    },
                    coerceValueCallback: null,
                    isAnimationProhibited: false),
                validateValueCallback: null);

        /// <summary>
        /// Description<br/>
        /// Default value: true
        /// </summary>
        [global::System.ComponentModel.Category("Category")]
        [global::System.ComponentModel.Description("Description")]
        public bool IsSpinning
        {
            get => (bool)GetValue(IsSpinningProperty);
            set => SetValue(IsSpinningProperty, value);
        }

        partial void OnIsSpinningChanged();
        partial void OnIsSpinningChanged(bool newValue);
        partial void OnIsSpinningChanged(bool oldValue, bool newValue);
    }
}
//HintName: TreeViewExtensions.AttachedProperties.SelectedItem.generated.cs

#nullable enable

namespace H.Generators.IntegrationTests
{
    public static partial class TreeViewExtensions
    {
        /// <summary>
        /// Default value: default(object)
        /// </summary>
        public static readonly global::System.Windows.DependencyProperty SelectedItemProperty =
            global::System.Windows.DependencyProperty.RegisterAttached(
                name: "SelectedItem",
                propertyType: typeof(object),
                ownerType: typeof(global::H.Generators.IntegrationTests.TreeViewExtensions),
                defaultMetadata: new global::System.Windows.FrameworkPropertyMetadata(
                    defaultValue: default(object),
                    flags: global::System.Windows.FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
                    propertyChangedCallback: static (sender, args) =>
                    {
                        OnSelectedItemChanged(
                            (global::System.Windows.Controls.TreeView)sender,
                            (object?)args.OldValue,
                            (object?)args.NewValue);
                    },
                    coerceValueCallback: null,
                    isAnimationProhibited: false),
                validateValueCallback: null);

        /// <summary>
        /// Default value: default(object)
        /// </summary>
        public static void SetSelectedItem(global::System.Windows.DependencyObject element, object? value)
        {
            element = element ?? throw new global::System.ArgumentNullException(nameof(element));

            element.SetValue(SelectedItemProperty, value);
        }

        /// <summary>
        /// Default value: default(object)
        /// </summary>
        [global::System.Windows.AttachedPropertyBrowsableForType(typeof(global::System.Windows.Controls.TreeView))]
        public static object? GetSelectedItem(global::System.Windows.DependencyObject element)
        {
            element = element ?? throw new global::System.ArgumentNullException(nameof(element));

            return (object?)element.GetValue(SelectedItemProperty);
        }

        static partial void OnSelectedItemChanged();
        static partial void OnSelectedItemChanged(global::System.Windows.Controls.TreeView treeView);
        static partial void OnSelectedItemChanged(global::System.Windows.Controls.TreeView treeView, object? newValue);
        static partial void OnSelectedItemChanged(global::System.Windows.Controls.TreeView treeView, object? oldValue, object? newValue);
    }
}

Advanced usage

new object/struct() expressions in DefaultValue

While it's generally not recommended to use new expressions in properties, a generator provides this capability:

public readonly record struct Data();

[AttachedDependencyProperty<object, TreeView>("SelectedItem", DefaultValueExpression = "new Data()")]

If your type is declared outside the namespace of an attribute declaration, you will need to specify the full name of the type, including the namespace.

XML documentation

The easiest way to add documentation is the Description parameter. It will add a System.ComponentModel.Description attribute to the property and will also be used in the xml documentation.
If for some reason you need to save raw xml documentation for your properties, there is an option to specify raw xml text for both DependencyProperty and getter/setter via XmlDocumentation/PropertyXmlDocumentation attribute properties.

Platform detection

For some platforms there is no automatic detection. In these cases, the generator needs a little help by adding:

  <PropertyGroup>
    <DefineConstants>$(DefineConstants);HAS_UNO</DefineConstants>
    <DefineConstants>$(DefineConstants);HAS_UNO_WINUI</DefineConstants>
    <DefineConstants>$(DefineConstants);HAS_AVALONIA</DefineConstants>
  </PropertyGroup>

Automatic detection of Uno was added in Uno 4.5.

Bind event

The generator can automatically control properties that depend on events:

[AttachedDependencyProperty<object, Grid>("BindEventProperty", BindEvent = nameof(Grid.MouseWheel))]
public static partial class GridExtensions
{
    private static void OnBindEventPropertyChanged_MouseWheel(object? sender, System.Windows.Input.MouseWheelEventArgs args)
    {
    }
}

will generate additional code:

static partial void OnBindEventPropertyChanged_BeforeBind(
    global::System.Windows.Controls.Grid sender,
    object? oldValue,
    object? newValue);
static partial void OnBindEventPropertyChanged_AfterBind(
    global::System.Windows.Controls.Grid sender,
    object? oldValue,
    object? newValue);

static partial void OnBindEventPropertyChanged(
    global::System.Windows.Controls.Grid sender,
    object? oldValue,
    object? newValue)
{
    OnBindEventPropertyChanged_BeforeBind(
        sender,
        oldValue,
        newValue);

    if (oldValue is not default(object))
    {
        sender.MouseWheel -= OnBindEventPropertyChanged_MouseWheel;
    }
    if (newValue is not default(object))
    {
        sender.MouseWheel += OnBindEventPropertyChanged_MouseWheel;
    }

    OnBindEventPropertyChanged_AfterBind(
        sender,
        oldValue,
        newValue);
}

Override metadata

For UWP/WinUI/Uno, a special RegisterPropertyChangedCallbacks() method will be created, which you will need to call in the constructor to register property change callbacks.

Notes

To use generic attributes, you need to set up LangVersion in your .csproj:

<LangVersion>preview</LangVersion>

There are also non-Generic attributes here.

Possible features

Support

Priority place for bugs: https://github.com/HavenDV/DependencyPropertyGenerator/issues
Priority place for ideas and general questions: https://github.com/HavenDV/DependencyPropertyGenerator/discussions
I also have a Discord support channel:
https://discord.gg/g8u2t9dKgE

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  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 netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net451 is compatible.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories (2)

Showing the top 2 popular GitHub repositories that depend on DependencyPropertyGenerator:

Repository Stars
JasonWei512/EnergyStarX
🔋 Improve your Windows 11 device's battery life. A WinUI 3 GUI for https://github.com/imbushuo/EnergyStar.
HavenDV/H.NotifyIcon
TrayIcon for WPF/WinUI/Uno/MAUI
Version Downloads Last updated
1.5.0-beta.2 888 7/23/2024
1.5.0-beta.1 2,336 1/16/2024
1.4.0 4,754 1/5/2024
1.4.0-alpha.3 292 12/4/2023
1.4.0-alpha.2 82 12/1/2023
1.4.0-alpha.1 71 11/30/2023
1.3.3 3,597 11/7/2023
1.3.2 2,121 9/23/2023
1.3.1 475 9/9/2023
1.3.0 669 9/1/2023
1.2.12 203 8/29/2023
1.2.11 136 8/29/2023
1.2.8 175 8/28/2023
1.2.7 171 8/27/2023
1.2.6 152 8/25/2023
1.2.5 2,266 3/24/2023
1.2.4 760 3/14/2023
1.2.3 651 3/11/2023
1.2.2 304 3/10/2023
1.2.1 256 3/9/2023
1.2.0 237 3/8/2023
1.1.7 242 3/6/2023
1.1.5 400 2/14/2023
1.1.4 407 2/2/2023
1.1.3 280 2/2/2023
1.1.2 622 1/28/2023
1.1.1 329 1/27/2023
1.1.0 305 1/27/2023
1.0.7 337 1/18/2023
1.0.6 447 1/9/2023
1.0.5 281 1/9/2023
1.0.3 2,547 8/26/2022
1.0.2 392 8/26/2022
1.0.1 448 8/23/2022
1.0.0 602 8/22/2022
0.53.0 637 8/13/2022
0.52.2 758 7/29/2022
0.52.1 441 7/29/2022
0.52.0 426 7/28/2022
0.51.1 436 7/28/2022
0.51.0 425 7/27/2022
0.50.1 438 7/26/2022
0.50.0 440 7/26/2022
0.49.0 398 7/25/2022
0.48.1 447 7/19/2022
0.48.0 445 7/19/2022
0.47.0 408 7/18/2022
0.46.0 455 7/18/2022
0.45.0 447 7/18/2022
0.44.0 421 7/18/2022
0.43.1 472 7/18/2022
0.43.0 442 7/18/2022
0.42.0 497 7/17/2022
0.41.0 469 7/17/2022
0.40.0 451 7/17/2022
0.39.0 457 7/16/2022
0.38.0 425 7/16/2022
0.37.0 447 7/13/2022
0.36.3 548 7/9/2022
0.36.2 418 7/9/2022
0.36.1 442 7/9/2022
0.36.0 430 7/9/2022
0.35.1 426 7/8/2022
0.35.0 453 7/8/2022
0.34.3 474 7/1/2022
0.34.2 432 7/1/2022
0.34.1 456 6/30/2022
0.34.0 400 6/30/2022
0.33.0 454 6/29/2022
0.32.1 485 6/24/2022
0.32.0 442 6/24/2022
0.31.0 462 6/24/2022
0.29.0 443 6/24/2022
0.28.1 442 6/24/2022
0.28.0 454 6/23/2022
0.27.5 480 6/23/2022
0.27.4 465 6/23/2022
0.27.3 461 6/23/2022
0.27.2 437 6/23/2022
0.27.1 442 6/23/2022
0.27.0 438 6/23/2022
0.26.1 423 6/23/2022
0.26.0 452 6/23/2022
0.25.0 460 6/22/2022
0.24.0 451 6/22/2022
0.23.0 464 6/22/2022
0.22.0 412 6/22/2022
0.21.1 423 6/22/2022
0.21.0 422 6/22/2022
0.20.0 415 6/22/2022
0.19.1 451 6/21/2022
0.19.0 452 6/21/2022
0.18.0 430 6/21/2022
0.16.5 441 6/20/2022
0.16.4 418 6/20/2022
0.16.2 402 6/20/2022
0.16.1 421 6/20/2022
0.16.0 442 6/20/2022
0.15.1 452 6/20/2022
0.15.0 454 6/20/2022
0.14.0 453 6/20/2022
0.12.0 460 6/20/2022
0.10.0 431 6/20/2022
0.9.0 427 6/20/2022
0.8.0 456 6/20/2022
0.7.3 453 6/19/2022
0.7.2 463 6/19/2022
0.7.1 432 6/19/2022
0.7.0 384 6/19/2022
0.6.0 431 6/19/2022
0.3.3 433 6/19/2022
0.3.2 464 6/19/2022
0.0.0-beta.274 70 8/19/2024
0.0.0-beta.273 69 8/19/2024
0.0.0-beta.272 62 8/12/2024
0.0.0-beta.271 37 8/5/2024
0.0.0-beta.270 48 7/29/2024
0.0.0-beta.269 53 7/23/2024

⭐ Last 10 features:
Added weak events support.
Added default xml doc for DependencyProperties as "Identifies xxx".
Added ability to set up custom OnChanged method.
Added OnChanged/OnChanging implementation detection to remove unused callbacks.
Added possibility of embedding attributes.
To one package.
Removed BindsTwoWayByDefault. Use DefaultBindingMode = DefaultBindingMode.TwoWay instead.
Added direct properties support for Avalonia.
Added DefaultBindingMode support for Avalonia/MAUI platforms.
Added PropertyChanging for MAUI.
🐞 Last 10 fixes:
Fixed RoutedEventGenerator and WeakEventGenerator platform detection after refactoring.
Fixed missing support for non-static weak events.
Added missing support of weak events for WPF.
Removed args parameter for WeakEvents RaiseEvent methods without type.
Fixed bug with multiline descriptions.
Fixed warnings with missing cref links in xml docs.
Fixed special characters in xml docs.
to net 7.0. Updated packages.
Added empty OnEvent() methods for WinRT platforms if WinRTEvents = false.
Added RoutedEventAttribute.WinRTEvents parameter to disable WinRT events by default.