O'Reilly logo

WPF Programmer's Reference: Windows Presentation Foundation with C# 2010 and .NET 4 by Rod Stephens

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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"/> ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required