DependencyPropertyGenerator 0.27.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package DependencyPropertyGenerator --version 0.27.1                
NuGet\Install-Package DependencyPropertyGenerator -Version 0.27.1                
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="0.27.1">
  <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 0.27.1                
#r "nuget: DependencyPropertyGenerator, 0.27.1"                
#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=0.27.1

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

DependencyPropertyGenerator

Dependency property source generator for WPF/UWP/WinUI/Uno platforms. For the WPF platform, RoutedEvents are also supported.

Install

Install-Package DependencyPropertyGenerator // Generator
Install-Package DependencyPropertyGenerator.Core // Attributes

Usage

using DependencyPropertyGenerator;
using System.Windows;
using System.Windows.Controls;

#nullable enable

namespace H.Generators.IntegrationTests;

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

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

will generate:

//HintName: MainWindow_DependencyProperties.generated.cs

#nullable enable

namespace H.Generators.IntegrationTests
{
    public partial class MainWindow
    {
        /// <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.MainWindow),
                typeMetadata: new global::System.Windows.FrameworkPropertyMetadata(
                    defaultValue: true,
                    flags: global::System.Windows.FrameworkPropertyMetadataOptions.None,
                    propertyChangedCallback: static (sender, args) =>
                        ((global::H.Generators.IntegrationTests.MainWindow)sender).OnIsSpinningChanged(
                            (bool)args.OldValue,
                            (bool)args.NewValue)));

        /// <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(bool oldValue, bool newValue);
    }
}
//HintName: TreeViewExtensions_AttachedDependencyProperties.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)));

        /// <summary>
        /// Default value: default(object)
        /// </summary>
        public static void SetSelectedItem(global::System.Windows.DependencyObject element, object? value)
        {
            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)
        {
            return (object?)element.GetValue(SelectedItemProperty);
        }

        static partial void OnSelectedItemChanged(global::System.Windows.Controls.TreeView sender, 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

If for some reason you need to save xml documentation for your properties, there is an option to specify xml text for both DependencyProperty and getter/setter via XmlDoc/PropertyXmlDoc attribute properties.

Notes

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

<LangVersion>preview</LangVersion>

There are also non-Generic attributes here.

Support

You can get answers to your questions in my discord support channel:
https://discord.gg/g8u2t9dKgE

There are no supported framework assets in this 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 1,135 7/23/2024
1.5.0-beta.1 2,372 1/16/2024
1.4.0 5,303 1/5/2024
1.4.0-alpha.3 298 12/4/2023
1.4.0-alpha.2 85 12/1/2023
1.4.0-alpha.1 74 11/30/2023
1.3.3 3,604 11/7/2023
1.3.2 2,148 9/23/2023
1.3.1 478 9/9/2023
1.3.0 681 9/1/2023
1.2.12 206 8/29/2023
1.2.11 139 8/29/2023
1.2.8 178 8/28/2023
1.2.7 174 8/27/2023
1.2.6 155 8/25/2023
1.2.5 2,270 3/24/2023
1.2.4 763 3/14/2023
1.2.3 655 3/11/2023
1.2.2 307 3/10/2023
1.2.1 259 3/9/2023
1.2.0 240 3/8/2023
1.1.7 245 3/6/2023
1.1.5 403 2/14/2023
1.1.4 410 2/2/2023
1.1.3 283 2/2/2023
1.1.2 625 1/28/2023
1.1.1 332 1/27/2023
1.1.0 308 1/27/2023
1.0.7 340 1/18/2023
1.0.6 450 1/9/2023
1.0.5 286 1/9/2023
1.0.3 2,556 8/26/2022
1.0.2 395 8/26/2022
1.0.1 451 8/23/2022
1.0.0 605 8/22/2022
0.53.0 640 8/13/2022
0.52.2 762 7/29/2022
0.52.1 444 7/29/2022
0.52.0 429 7/28/2022
0.51.1 439 7/28/2022
0.51.0 428 7/27/2022
0.50.1 441 7/26/2022
0.50.0 443 7/26/2022
0.49.0 401 7/25/2022
0.48.1 450 7/19/2022
0.48.0 450 7/19/2022
0.47.0 411 7/18/2022
0.46.0 458 7/18/2022
0.45.0 450 7/18/2022
0.44.0 424 7/18/2022
0.43.1 477 7/18/2022
0.43.0 445 7/18/2022
0.42.0 500 7/17/2022
0.41.0 472 7/17/2022
0.40.0 454 7/17/2022
0.39.0 460 7/16/2022
0.38.0 428 7/16/2022
0.37.0 450 7/13/2022
0.36.3 551 7/9/2022
0.36.2 421 7/9/2022
0.36.1 445 7/9/2022
0.36.0 433 7/9/2022
0.35.1 429 7/8/2022
0.35.0 456 7/8/2022
0.34.3 477 7/1/2022
0.34.2 435 7/1/2022
0.34.1 459 6/30/2022
0.34.0 405 6/30/2022
0.33.0 457 6/29/2022
0.32.1 488 6/24/2022
0.32.0 445 6/24/2022
0.31.0 465 6/24/2022
0.29.0 446 6/24/2022
0.28.1 445 6/24/2022
0.28.0 457 6/23/2022
0.27.5 483 6/23/2022
0.27.4 468 6/23/2022
0.27.3 464 6/23/2022
0.27.2 440 6/23/2022
0.27.1 445 6/23/2022
0.27.0 441 6/23/2022
0.26.1 426 6/23/2022
0.26.0 455 6/23/2022
0.25.0 463 6/22/2022
0.24.0 456 6/22/2022
0.23.0 467 6/22/2022
0.22.0 415 6/22/2022
0.21.1 426 6/22/2022
0.21.0 425 6/22/2022
0.20.0 418 6/22/2022
0.19.1 454 6/21/2022
0.19.0 455 6/21/2022
0.18.0 433 6/21/2022
0.16.5 444 6/20/2022
0.16.4 421 6/20/2022
0.16.2 405 6/20/2022
0.16.1 424 6/20/2022
0.16.0 447 6/20/2022
0.15.1 455 6/20/2022
0.15.0 457 6/20/2022
0.14.0 456 6/20/2022
0.12.0 463 6/20/2022
0.10.0 434 6/20/2022
0.9.0 430 6/20/2022
0.8.0 459 6/20/2022
0.7.3 458 6/19/2022
0.7.2 466 6/19/2022
0.7.1 435 6/19/2022
0.7.0 389 6/19/2022
0.6.0 434 6/19/2022
0.3.3 436 6/19/2022
0.3.2 467 6/19/2022
0.0.0-beta.274 73 8/19/2024
0.0.0-beta.273 72 8/19/2024
0.0.0-beta.272 65 8/12/2024
0.0.0-beta.271 40 8/5/2024
0.0.0-beta.270 51 7/29/2024
0.0.0-beta.269 56 7/23/2024

⭐ Last 10 features:
Added DebuggerBreak property to debug external libraries.
Added Bindable and DesignerSerializationVisibility attributes support.
Added attached routed events support for WPF platform.
Added RoutedEvent support for WPF platform.
Added TypeConverterAttribute support.
Added partial Localizability support for WPF platform.
Added CLSCompliant attribute support.
Added global:: prefixes to all types.
Added ability to setup Category and Description attributes.
Simplified common dependency property OnChanged method.
🐞 Last 10 bug fixes:
Fixed DebuggerBreak in design-time.
To file-per-property/event generation.
Fixed bug with default enum value.
Fixed problem with failed tests.
Fixed bug with multuple class declarations.
Changed detection order to fix Uno.WinUI detection.
Fixed platform detection through constants.
Fixed Uno parameter name bug.
Fixed UWP/Uno/UnoWinUI detection.
Fixed incorrect DependencyObject type in new platforms.