Setter Precedence
Avalonia Setters
are applied in order of BindingPriority
, then visual tree locality, and finally the Styles
collection
order. Precedence applies individually to each StyledProperty
so that styling can benefit from composition. DirectProperty
and CLR properties cannot be styled and therefore do not participate in this precedence.
BindingPriority Values
Animation = -1, // Highest priority
LocalValue = 0,
StyleTrigger,
Template,
Style,
Inherited,
Unset = int.MaxValue, // Lowest priority
How is BindingPriority Assigned Within XAML?
BindingPriority
cannot be explicitly set in XAML. The following examples demonstrate how BindingPriority
is
implicitly assigned across each scenario. This is crucial for designing and troubleshooting styles that work as expected.
Animation
Animation
has the highest BindingPriority
and is applied to Setter
s within a Keyframe
and generally throughout the
Transitions system.
<Button Background="Green" Content="Bounces from Red to Blue">
<Button.Styles>
<Style Selector="Button">
<Style.Animations>
<Animation IterationCount="Infinite" Duration="0:0:2">
<KeyFrame Cue="0%">
<Setter Property="Background" Value="Red" />
</KeyFrame>
<KeyFrame Cue="100%">
<Setter Property="Background" Value="Blue" />
</KeyFrame>
</Animation>
</Style.Animations>
</Style>
</Button.Styles>
</Button>
LocalValue
Assigned when a XAML property is directly set outside of a ControlTemplate
. Both Background
Setter
s below will
have LocalValue
priority.
<Button Background="Orange" />
<Button Background="{DynamicResource ButtonBrush}" />
Resource markup extensions do not have any effect on priority.
StyleTrigger
When a Selector
has conditional activation, the Setter
's BindingPriority
is promoted from Style
to
StyleTrigger
. Two selectors with any conditional activation will have equal priority regardless of the number of
activators present and the position of the activator within the selector syntax. Avalonia doesn't have CSS's concept
of Specificity.
<Style Selector="Button:pointerover /template/ ContentPresenter#PART_ContentPresenter">
<Setter Property="Background" Value="Orange" />
</Style>
Style class, pseudo class, child position, and property match selectors are conditional. Control name selectors are not conditional.
Template
When a property is directly set within a ControlTemplate
. BorderThickness
, Background
, and Padding
below have Template
priority.
<ControlTemplate>
<Border BorderThickness="2">
<Button Background="{DynamicResource ButtonBrush}" Padding="{TemplateBinding Padding}" />
</Border>
</ControlTemplate>
Style
When a Setter
is defined within a Style
without conditional activation.
<Style Selector="Button /template/ ContentPresenter#PART_ContentPresenter">
<Setter Property="Background" Value="Orange" />
</Style>
Especially noteworthy is the lower priority than Template
. Therefore, these selectors cannot be used to override the
properties mentioned in the Template
example above.
Inherited
When a property is not set, it may inherit the property value from its parent. This must be specified during
property registration or with OverrideMetadata
.
public static readonly StyledProperty<bool> UseLayoutRoundingProperty =
AvaloniaProperty.Register<Layoutable, bool>(
nameof(UseLayoutRounding),
defaultValue: true,
inherits: true);