Home

Awesome

Roslyn source generator to create dependency and attached properties for WPF, Xamarin.Forms, and .NET MAUI

NuGet Bindables.Wpf NuGet Bindables.Forms NuGet Bindables.Forms AppVeyor

Requirements

Example (WPF)

Check Types for Frameworks for other frameworks.

Your Code

using System.Windows;
using Bindables.Wpf;

public partial class YourClass : DependencyObject
{
    private static readonly string DefaultValue = "Test";

    [DependencyProperty(typeof(string))]
    public static readonly DependencyProperty RegularProperty;

    // You can use any visibility modifier.
    [DependencyProperty(typeof(string))]
    private static readonly DependencyPropertyKey ReadOnlyPropertyKey;

    [DependencyProperty(typeof(string), OnPropertyChanged = nameof(PropertyChangedCallback), DefaultValueField = nameof(DefaultValue), Options = FrameworkPropertyMetadataOptions.BindsTwoWayByDefault)]
    public static readonly DependencyProperty CustomizedProperty;

    private static void PropertyChangedCallback(DependencyObject obj, DependencyPropertyChangedEventArgs args)
    {
    }
}

Generated Code

// Generated by Bindables
using System.Windows;

#nullable enable

public partial class YourClass
{
    [global::System.CodeDom.Compiler.GeneratedCode("Bindables.Wpf.WpfPropertyGenerator", "1.4.1")]
    public string? Regular
    {
        get => (string?)GetValue(RegularProperty);
        set => SetValue(RegularProperty, value);
    }

    [global::System.CodeDom.Compiler.GeneratedCode("Bindables.Wpf.WpfPropertyGenerator", "1.4.1")]
    public static readonly DependencyProperty ReadOnlyProperty;

    [global::System.CodeDom.Compiler.GeneratedCode("Bindables.Wpf.WpfPropertyGenerator", "1.4.1")]
    public string? ReadOnly
    {
        get => (string?)GetValue(ReadOnlyProperty);
        private set => SetValue(ReadOnlyPropertyKey, value);
    }

    [global::System.CodeDom.Compiler.GeneratedCode("Bindables.Wpf.WpfPropertyGenerator", "1.4.1")]
    public string? Customized
    {
        get => (string?)GetValue(CustomizedProperty);
        set => SetValue(CustomizedProperty, value);
    }

    [global::System.CodeDom.Compiler.GeneratedCode("Bindables.Wpf.WpfPropertyGenerator", "1.4.1")]
    static YourClass()
    {
        RegularProperty = DependencyProperty.Register(
            nameof(Regular),
            typeof(string),
            typeof(YourClass),
            new FrameworkPropertyMetadata());

        ReadOnlyPropertyKey = DependencyProperty.RegisterReadOnly(
            nameof(ReadOnly),
            typeof(string),
            typeof(YourClass),
            new FrameworkPropertyMetadata());

        ReadOnlyProperty = ReadOnlyPropertyKey.DependencyProperty;

        CustomizedProperty = DependencyProperty.Register(
            nameof(Customized),
            typeof(string),
            typeof(YourClass),
            new FrameworkPropertyMetadata(DefaultValue, (FrameworkPropertyMetadataOptions)256, PropertyChangedCallback));
    }
}

Options

You can pass following options:

OptionDescription
OnPropertyChangedName of the method that will be called when the property is changed.
OnCoerceValue (WPF)Name of the method that will be called when the property is re-evaluated/coerced.
DefaultValueFieldName of the static field that will provide the default value for the property.
Options (WPF)Pass System.Windows.FrameworkPropertyMetadataOptions to the dependency property.
BindingMode (Xamarin.Forms)Pass Xamarin.Forms.BindingMode to the dependency property.
BindingMode (.NET MAUI)Pass Microsoft.Maui.Controls.BindingMode to the dependency property.

Signature of OnPropertyChanged method should be:

Project TypeSignature
WPFstatic void MethodName(DependencyObject obj, DependencyPropertyChangedEventArgs args)
Xamarin.Forms & .NET MAUIstatic void MethodName(BindableObject obj, object oldValue, object newValue)

Signature of OnCoerceValue method should be:

Project TypeSignature
WPFstatic object MethodName(DependencyObject obj, object value)
Xamarin.Forms & .NET MAUIstatic object MethodName(BindableObject obj, object value)

Types for Frameworks

Project TypeDependency Property TypeAccess TypeProperty SuffixField TypeAttribute Type
WPFDependency PropertyRead/WritePropertyDependencyPropertyBindables.Wpf.DependencyPropertyAttribute
WPFDependency PropertyRead OnlyPropertyKeyDependencyPropertyKeyBindables.Wpf.DependencyPropertyAttribute
WPFAttached PropertyRead/WritePropertyDependencyPropertyBindables.Wpf.AttachedPropertyAttribute
WPFAttached PropertyRead OnlyPropertyKeyDependencyPropertyKeyBindables.Wpf.AttachedPropertyAttribute
Xamarin.FormsBindable PropertyRead/WritePropertyBindablePropertyBindables.Forms.BindablePropertyAttribute
Xamarin.FormsBindable PropertyRead OnlyPropertyKeyBindablePropertyKeyBindables.Forms.BindablePropertyAttribute
Xamarin.FormsAttached PropertyRead/WritePropertyBindablePropertyBindables.Forms.AttachedPropertyAttribute
Xamarin.FormsAttached PropertyRead OnlyPropertyKeyBindablePropertyKeyBindables.Forms.AttachedPropertyAttribute
.NET MAUIBindable PropertyRead/WritePropertyBindablePropertyBindables.Maui.BindablePropertyAttribute
.NET MAUIBindable PropertyRead OnlyPropertyKeyBindablePropertyKeyBindables.Maui.BindablePropertyAttribute
.NET MAUIAttached PropertyRead/WritePropertyBindablePropertyBindables.Maui.AttachedPropertyAttribute
.NET MAUIAttached PropertyRead OnlyPropertyKeyBindablePropertyKeyBindables.Maui.AttachedPropertyAttribute