Primary WinForms APIs:
UserControlControl subclasses with custom properties and paintingPrimary Avalonia APIs:
UserControl for composition-first controlsTemplatedControl for skinnable reusable controlsStyledProperty, DirectProperty)| WinForms | Avalonia |
|---|---|
UserControl with child controls |
UserControl with XAML composition |
custom Control + OnPaint |
TemplatedControl + template + optional Render |
| CLR properties only | register Avalonia properties for styling/binding |
WinForms C#:
public partial class HeaderPanel : UserControl
{
public string TitleText
{
get => titleLabel.Text;
set => titleLabel.Text = value;
}
}
Avalonia XAML (HeaderPanel.axaml):
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MyApp.Controls"
x:Class="MyApp.Controls.HeaderPanel">
<Border Padding="8" Classes="header-panel">
<TextBlock Text="{Binding TitleText, RelativeSource={RelativeSource AncestorType=local:HeaderPanel}}" />
</Border>
</UserControl>
using Avalonia;
using Avalonia.Controls.Primitives;
public class HeaderPanel : TemplatedControl
{
public static readonly StyledProperty<string> TitleTextProperty =
AvaloniaProperty.Register<HeaderPanel, string>(nameof(TitleText), string.Empty);
public string TitleText
{
get => GetValue(TitleTextProperty);
set => SetValue(TitleTextProperty, value);
}
}
A template for HeaderPanel can then be provided via ControlTheme in app styles.
TemplatedControl where needed.