Repository navigation
[MVVM] AttachedProperty/DirectProperty/StyledProperty source generators #1203
Copy link
Copy link
Open
Labels
feature request 📬A request for new changes to improve functionalityA request for new changes to improve functionality
Description
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
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
feature request 📬A request for new changes to improve functionalityA request for new changes to improve functionality
Activity