Primary WPF APIs:
UserControlControl subclasses with DefaultStyleKey metadata overridePrimary Avalonia APIs:
UserControlTemplatedControlControlTheme, template parts, styled/direct properties| WPF | Avalonia |
|---|---|
UserControl |
UserControl |
custom control with DefaultStyleKey override |
TemplatedControl + ControlTheme |
| dependency properties | Avalonia styled/direct properties |
WPF C#:
public class BadgeControl : Control
{
static BadgeControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(BadgeControl),
new FrameworkPropertyMetadata(typeof(BadgeControl)));
}
}
Avalonia XAML (Styles.axaml):
<ControlTheme xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MyApp.Controls"
x:Key="BadgeControlTheme"
TargetType="local:BadgeControl">
<Setter Property="Template">
<ControlTemplate>
<Border Background="{TemplateBinding Background}" CornerRadius="10" Padding="8,2">
<ContentPresenter Content="{TemplateBinding Content}" />
</Border>
</ControlTemplate>
</Setter>
</ControlTheme>
Usage:
<local:BadgeControl xmlns="https://github.com/avaloniaui"
xmlns:local="using:MyApp.Controls"
Theme="{StaticResource BadgeControlTheme}"
Content="New" />
using Avalonia;
using Avalonia.Controls.Primitives;
public class BadgeControl : TemplatedControl
{
public static readonly StyledProperty<object?> ContentProperty =
AvaloniaProperty.Register<BadgeControl, object?>(nameof(Content));
public object? Content
{
get => GetValue(ContentProperty);
set => SetValue(ContentProperty, value);
}
}
ControlTheme exists and is loaded into app styles.StyledProperty for values intended to be set by styles.OnApplyTemplate.