C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Triggers: I add a sub-element to the Rectangle called Rectangle.Triggers. This is complicated, but you just have to remember the elements.
RoutedEvent: The animation begins when the Loaded event is triggered on the Window. We use EventTrigger for this.
Example markup: XAML
<Window x:Class="WpfApplication28.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
Loaded="Window_Loaded">
<Canvas Name="Can1">
<Rectangle Name="Rect1" Canvas.Left="10" Fill="LightSeaGreen"
Stroke="Bisque"
StrokeThickness="5"
Width="100" Height="100">
<Rectangle.Triggers>
<EventTrigger RoutedEvent="Window.Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetName="Rect1"
Storyboard.TargetProperty="(Canvas.Left)"
From="10" To="100"
Duration="0:0:2"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Rectangle.Triggers>
</Rectangle>
</Canvas>
</Window>
From, To: We can adjust the "From" and the "To" to change the animation's start and end values. These are used for the TargetProperty.
PropertySyntax: Please add the parentheses around the TargetProperty in your XAML. If you do not, an error (a confusing one) will occur.
Tip: These elements at first make WPF seem excessively complicated and impenetrable. But when needed, they make programs simpler.