Templated Control
A generic control can be restyled.
TIP
All most all built-in components are templated from avalonia, so it's possible to inherit from them and customize on your design.
Template
The slot as <ControlTemplate />
is the value of the style.
xml
<Styles xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="using:AvaloniaApplication">
<Design.PreviewWith>
<controls:MyTemplatedControl />
</Design.PreviewWith>
<Style Selector="controls|MyTemplatedControl">
<!-- Set Defaults -->
<Setter Property="Template">
<ControlTemplate>
<TextBlock Text="Templated Control" />
</ControlTemplate>
</Setter>
</Style>
</Styles>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
INFO
Use |
to access members of a xml namespace in selector syntax.
Code behind
cs
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
namespace AvaloniaApplication;
public class MyTemplatedControl : TemplatedControl
{
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
Each templated control inherits from TemplatedControl
, and it has a TemplateProperty
where we set our template.
cs
public class TemplatedControl : Control
{
// ...
public IControlTemplate? Template
{
get { return GetValue(TemplateProperty); }
set { SetValue(TemplateProperty, value); }
}
// ...
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10