Control Animations Through Triggers


   
     

<Window x:Class="Main"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="" Height="350" Width="400">
  <Window.Resources>
    <Storyboard x:Key="Storyboard" RepeatBehavior="10x">
      <DoubleAnimation Storyboard.TargetName="rect1" Storyboard.TargetProperty="Width" To="250" FillBehavior="HoldEnd" AutoReverse="False" />
      <DoubleAnimation Storyboard.TargetName="rect2" Storyboard.TargetProperty="Width" To="250" AutoReverse="True" />
      <ColorAnimation Storyboard.TargetName="ellipse1" Storyboard.TargetProperty="Fill.(SolidColorBrush.Color)" To="Orange"
        AutoReverse="True" />
    </Storyboard>
  </Window.Resources>

  <Window.Triggers>
    <EventTrigger RoutedEvent="Button.Click" SourceName="btnBegin">
      <BeginStoryboard x:Name="beginStoryboard" Storyboard="{StaticResource Storyboard}" />
    </EventTrigger>
    <EventTrigger RoutedEvent="Button.Click" SourceName="btnPause">
    <PauseStoryboard BeginStoryboardName="beginStoryboard" />
    </EventTrigger>
    <EventTrigger RoutedEvent="Button.Click" SourceName="btnResume">
      <ResumeStoryboard BeginStoryboardName="beginStoryboard" />
    </EventTrigger>
    <EventTrigger RoutedEvent="Button.Click" SourceName="btnStop">
      <StopStoryboard BeginStoryboardName="beginStoryboard" />
    </EventTrigger>

    <EventTrigger RoutedEvent="Button.Click" SourceName="btnSeek">
      <SeekStoryboard BeginStoryboardName="beginStoryboard" Offset="0:0:5" Origin="BeginTime" />
    </EventTrigger>

    <EventTrigger RoutedEvent="Button.Click" SourceName="btnSkipToFill">
      <SkipStoryboardToFill BeginStoryboardName="beginStoryboard" />
    </EventTrigger>
    <EventTrigger RoutedEvent="Button.Click" SourceName="btnDoubleSpeed">
      <SetStoryboardSpeedRatio BeginStoryboardName="beginStoryboard" SpeedRatio="2" />
    </EventTrigger>

    <EventTrigger RoutedEvent="Button.Click" SourceName="btnHalfSpeed">
      <SetStoryboardSpeedRatio BeginStoryboardName="beginStoryboard" SpeedRatio="0.5" />
    </EventTrigger>

  </Window.Triggers>
  <Grid>
    <StackPanel>
      <Rectangle x:Name="rect1" Width="50" Height="100" Stroke="Black" Fill="CornflowerBlue" Margin="5" />
      <Ellipse x:Name="ellipse1" Width="50" Height="50" Stroke="Black" Fill="Firebrick" StrokeThickness="1" Margin="5" />
      <Rectangle x:Name="rect2" Width="50" Height="100" Stroke="Black" Fill="CornflowerBlue" Margin="5" />
    </StackPanel>
    <StackPanel Orientation="Horizontal">
        <Button x:Name="btnBegin" Content="Begin" />
        <Button x:Name="btnPause" Content="Pause" />
        <Button x:Name="btnResume" Content="Resume" />
        <Button x:Name="btnStop" Content="Stop" />
        <Button x:Name="btnSeek" Content="Seek" />
        <Button x:Name="btnSkipToFill" Content="Skip To Fill" />
        <Button x:Name="btnDoubleSpeed" Content="Double Speed" />
        <Button x:Name="btnHalfSpeed" Content="Half Speed" />
    </StackPanel>    
  </Grid>
</Window>