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
Overview
Examples like these require a considerable amount of repetitive code:
API breakdown
Usage example
QuxControl.csMyProject.Views.QuxControl.g.csBreaking 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