Appendix O. Triggers and Animation

This appendix summarizes triggers and animations. For more detailed information, see Chapter 14.

EventTriggers

You can place EventTriggers inside a control's Triggers section to take action when an event occurs. Inside the EventTrigger, you can place actions that should occur when the event takes place.

The most useful of these actions include:

  • PauseStoryboard — Pauses a Storyboard.

  • ResumeStoryboard — Resumes a paused Storyboard.

  • SeekStoryboard — Moves the Storyboard to a specific position in its timeline.

  • StopStoryboard — Stops a Storyboard. This resets properties to their original values.

  • RemoveStoryboard — Stops a Storyboard and frees its resources.

The following code gives a Button an EventTrigger. When the Button raises its Click event, the EventTrigger starts a Storyboard that changes the Button's Canvas.Left property to 300 over a period of 0.5 seconds. The animation's AutoReverse property is True, so after the animation completes, it changes the Canvas.Left property back to its original value.

<Button Name="btnClickMe" Canvas.Left="10" Canvas.Top="10" Content="Click Me">
    <Button.Triggers>
<EventTrigger RoutedEvent="Button.Click"> <EventTrigger.Actions> <BeginStoryboard> <Storyboard> <DoubleAnimation Duration="0:0:0.5" To="300" Storyboard.TargetProperty="(Canvas.Left)" AutoReverse="True"/> ...

Get WPF Programmer's Reference: Windows Presentation Foundation with C# 2010 and .NET 4 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.