Skip to content

[MVVM] AttachedProperty/DirectProperty/StyledProperty source generators #1203

Description

@ero-qt

Overview

Examples like these require a considerable amount of repetitive code:

public bool Foo
{
  get => GetValue(FooProperty);
  set => SetValue(FooProperty, value);
}

public ObservableCollection<BarViewModel>? BarsSource
{
  get => GetValue(BarsSourceProperty);
  set => SetValue(BarsSourceProperty, value);
}

public ICommand? BazCommand
{
  get => GetValue(BazCommandProperty);
  set => SetValue(BazCommandProperty, value);
}

public static readonly StyledProperty<bool> FooProperty =
  AvaloniaProperty.Register<QuxControl, bool>(nameof(Foo));

public static readonly StyledProperty<ObservableCollection<BarViewModel>?> BarsSourceProperty =
  AvaloniaProperty.Register<QuxControl, ObservableCollection<BarViewModel>?>(nameof(BarsSource));

public static readonly StyledProperty<ICommand?> BazCommandProperty =
  AvaloniaProperty.Register<QuxControl, ICommand?>(nameof(BazCommand));

API breakdown

namespace CommunityToolkit.Mvvm.ComponentModel;

[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public sealed class AttachedPropertyAttribute : Attribute;

[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public sealed class DirectPropertyAttribute : Attribute;

[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public sealed class StyledPropertyAttribute : Attribute;

Usage example

QuxControl.cs

public partial class QuxControl : Panel
{
  [StyledProperty]
  public partial bool Foo { get; set; }

  [StyledProperty]
  public partial ObservableCollection<BarViewModel>? BarsSource { get; set; }

  [StyledProperty]
  public partial ICommand? BazCommand { get; set; }
}

MyProject.Views.QuxControl.g.cs

namespace MyProject.Views
{
  partial class QuxControl
  {
    /* ... */
    public partial bool Foo
    {
      get => GetValue(FooProperty);
      set => SetValue(FooProperty, value);
    }

    /* ... */
    public partial ObservableCollection<BarViewModel>? BarsSource
    {
      get => GetValue(BarsSourceProperty);
      set => SetValue(BarsSourceProperty, value);
    }

    /* ... */
    public partial ICommand? BazCommand
    {
      get => GetValue(BazCommandProperty);
      set => SetValue(BazCommandProperty, value);
    }

    /* ... */
    public static readonly StyledProperty<bool> FooProperty =
      AvaloniaProperty.Register<QuxControl, bool>(nameof(Foo));

    /* ... */
    public static readonly StyledProperty<ObservableCollection<BarViewModel>?> BarsSourceProperty =
      AvaloniaProperty.Register<QuxControl, ObservableCollection<BarViewModel>?>(nameof(BarsSource));

    /* ... */
    public static readonly StyledProperty<ICommand?> BazCommandProperty =
      AvaloniaProperty.Register<QuxControl, ICommand?>(nameof(BazCommand));
  }
}

Breaking change?

No

Alternatives

There are existing packages (https://github.com/jp2masa/Avalonia.PropertyGenerator, https://github.com/lucdem/AvaSourceGenerators) that don't entirely work in the proposed way.

Additional context

No response

Help us help you

No, just wanted to propose this

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature request 📬A request for new changes to improve functionality

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions