Styles
Styles in Avalonia are used to share property settings between controls. The Avalonia styling system can be thought of as a mix of CSS styling and WPF/UWP styling. At its most basic, a style consists of a selector and a collection of setters.
A style applies to the control that it is defined on and all descendent controls.
The following style selects any TextBlock
with a h1
style class and sets its font size to 24 point and font weight to bold:
<Style Selector="TextBlock.h1">
<Setter Property="FontSize" Value="24"/>
<Setter Property="FontWeight" Value="Bold"/>
</Style>
Styles can be defined on any control or on the Application
object by adding them to the Control.Styles
or Application.Styles
collections.
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Window.Styles>
<Style Selector="TextBlock.h1">
<Setter Property="FontSize" Value="24"/>
<Setter Property="FontWeight" Value="Bold"/>
</Style>
</Window.Styles>
<TextBlock Classes="h1">I'm a Heading!</TextBlock>
</Window>
Styles can also be included from other files using the StyleInclude
class, e.g.:
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Window.Styles>
<StyleInclude Source="/CustomStyles.xaml" />
</Window.Styles>
<TextBlock Classes="h1">I'm a Heading!</TextBlock>
</Window>
Where CustomStyles.xaml
is a XAML file with a root of either Style
or Styles
and is included as an asset in the application, e.g.:
CustomStyles.xaml
<Styles xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style Selector="TextBlock.h1">
...
</Style>
</Styles>
Note that unlike WPF/UWP, styles will have no effect if they're added to a control or application ResourceDictionary
. This is because the order that styles are defined in Avalonia is important and ResourceDictionary
is an unsorted dictionary.
Style Classes
As in CSS, controls can be given style classes which can be used in selectors. Style classes can be assigned in XAML by setting the Classes
property to a space-separated list of strings. The following example applies the h1
and blue
style classes to a Button
:
<Button Classes="h1 blue"/>
If you need to add or remove class by condition, you can use the following special syntax:
<Button Classes.blue="{Binding IsSpecial}" />
Style classes can also be manipulated in code using the Classes
collection:
control.Classes.Add("blue");
control.Classes.Remove("red");
Pseudoclasses
Also as in CSS, controls can have pseudoclasses; these are classes that are defined by the control itself rather than by the user. Pseudoclasses start with a :
character.
One example of a pseudoclass is the :pointerover
(similar to :hover
in CSS).
Pseudoclasses provide the functionality of Triggers
in WPF and VisualStateManager
in UWP:
<StackPanel>
<StackPanel.Styles>
<Style Selector="Border:pointerover">
<Setter Property="Background" Value="Red"/>
</Style>
</StackPanel.Styles>
<Border>
<TextBlock>I will have red background when hovered.</TextBlock>
</Border>
</StackPanel>
Another example that involves changing properties inside of control template:
<StackPanel>
<StackPanel.Styles>
<Style Selector="Button:pressed /template/ ContentPresenter">
<Setter Property="TextBlock.Foreground" Value="Red"/>
</Style>
</StackPanel.Styles>
<Button>I will have red text when pressed.</Button>
</StackPanel>
Other pseudoclasses include :focus
, :disabled
, :pressed
for buttons, :checked
for checkboxes etc.