Transitions
Transitions in Avalonia are also heavily inspired by CSS Animations. They listen to any changes in target property's value and subsequently animates the change according to its parameters. They can be defined on any Control via Transitions property:
1
<Window xmlns="https://github.com/avaloniaui">
2
<Window.Styles>
3
<Style Selector="Rectangle.red">
4
<Setter Property="Height" Value="100"/>
5
<Setter Property="Width" Value="100"/>
6
<Setter Property="Fill" Value="Red"/>
7
<Setter Property="Opacity" Value="0.5"/>
8
</Style>
9
<Style Selector="Rectangle.red:pointerover">
10
<Setter Property="Opacity" Value="1"/>
11
</Style>
12
</Window.Styles>
13
​
14
<Rectangle Classes="red">
15
<Rectangle.Transitions>
16
<Transitions>
17
<DoubleTransition Property="Opacity" Duration="0:0:0.2"/>
18
</Transitions>
19
</Rectangle.Transitions>
20
</Rectangle>
21
​
22
</Window>
Copied!
The above example will listen to changes in the Rectangle's Opacity property, and when the value changes, apply a smooth transition from the old value to the new value over 2 seconds.
Transitions can also be defined in any style by using a Setter with Transitions as the target property and encapsulating them in a Transitions object, like so:
1
<Window xmlns="https://github.com/avaloniaui">
2
<Window.Styles>
3
<Style Selector="Rectangle.red">
4
<Setter Property="Height" Value="100"/>
5
<Setter Property="Width" Value="100"/>
6
<Setter Property="Fill" Value="Red"/>
7
<Setter Property="Opacity" Value="0.5"/>
8
<Setter Property="Transitions">
9
<Transitions>
10
<DoubleTransition Property="Opacity" Duration="0:0:0.2"/>
11
</Transitions>
12
</Setter>
13
</Style>
14
<Style Selector="Rectangle.red:pointerover">
15
<Setter Property="Opacity" Value="1"/>
16
</Style>
17
</Window.Styles>
18
​
19
<Rectangle Classes="red"/>
20
​
21
</Window>
Copied!
Every transition has a Property, Delay, Duration and an optional Easing property.
Property refers to a transition's target for listening and animating values upon.
Delay refers to the amount of time before the transition is applied to the target.
Duration refers to the amount of time that the transition plays.
The easing functions are the same as those described in Keyframe Animations.
The following transition types are available. The correct type must be used depending on the type of the property being animated.
  • BoxShadowsTransition: For BoxShadows target properties
  • BrushTransition: For IBrush target properties
  • ColorTransition: For Color target properties
  • CornerRadiusTransition: For CornerRadius target properties
  • DoubleTransitions: For double target properties
  • FloatTransitions: For float target properties
  • IntegerTransitions: For int target properties
  • PointTransition: For Point target properties
  • SizeTransition: For Size target properties
  • ThicknessTransition: For Thickness target properties
  • TransformOperationsTransition: For ITransform target properties
  • VectorTransition: For Vector target properties

Transitioning Render Transforms

Render transforms applied to controls using CSS-like syntax can be transitioned. The following example shows a Border which rotates 45 degrees when the pointer is hovered over it:
XAML
C#
1
<Border Width="100" Height="100" Background="Red">
2
<Border.Styles>
3
<Style Selector="Border">
4
<Setter Property="RenderTransform" Value="rotate(0)"/>
5
</Style>
6
<Style Selector="Border:pointerover">
7
<Setter Property="RenderTransform" Value="rotate(45deg)"/>
8
</Style>
9
</Border.Styles>
10
<Border.Transitions>
11
<Transitions>
12
<TransformOperationsTransition Property="RenderTransform" Duration="0:0:1"/>
13
</Transitions>
14
</Border.Transitions>
15
</Border>
Copied!
1
new Border
2
{
3
Width = 100,
4
Height = 100,
5
Background = Brushes.Red,
6
Styles =
7
{
8
new Style(x => x.OfType<Border>())
9
{
10
Setters =
11
{
12
new Setter(
13
Border.RenderTransformProperty,
14
TransformOperations.Parse("rotate(0)"))
15
},
16
},
17
new Style(x => x.OfType<Border>().Class(":pointerover"))
18
{
19
Setters =
20
{
21
new Setter(
22
Border.RenderTransformProperty,
23
TransformOperations.Parse("rotate(45deg)"))
24
},
25
},
26
},
27
Transitions = new Transitions
28
{
29
new TransformOperationsTransition
30
{
31
Property = Border.RenderTransformProperty,
32
Duration = TimeSpan.FromSeconds(1),
33
}
34
}
35
};
Copied!
Avalonia also supports WPF-style render transforms such asRotateTransform ,ScaleTransform etc. These transforms cannot be transitioned: always use the CSS-like format if you want to apply a transition to a render transform.
Export as PDF
Copy link
Edit on GitHub