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>






     


Animate the Color of a Brush with Indirect Property Targeting


   
     
<Window x:Class="Main"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="" Height="300" Width="300"
  Background="Black">
  <Window.Triggers>
    <EventTrigger RoutedEvent="Window.Loaded">
      <BeginStoryboard>
        <Storyboard AutoReverse="True" RepeatBehavior="Forever">
          <ColorAnimation Storyboard.TargetProperty="Background.Color" To="White" />
          <ColorAnimation Storyboard.TargetName="bd" Storyboard.TargetProperty="Background.(SolidColorBrush.Color)" To="Black" />
        </Storyboard>
      </BeginStoryboard>
    </EventTrigger>
  </Window.Triggers>
  <Border x:Name="bd" Margin="20" Background="HotPink">
    <Rectangle x:Name="rect" Width="100" Height="100" Fill="WhiteSmoke" />
  </Border>
</Window>






     


Animate the background color


   
     

<Window x:Class="Main"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="" Height="300" Width="300"
  Background="Black">
  <Window.Triggers>
    <EventTrigger RoutedEvent="Window.Loaded">
      <BeginStoryboard>
        <Storyboard AutoReverse="True" RepeatBehavior="Forever">
          <ColorAnimation Storyboard.TargetProperty="Background.Color" To="White" />
          <ColorAnimation Storyboard.TargetName="rect" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)" To="Firebrick" />
        </Storyboard>
      </BeginStoryboard>
    </EventTrigger>
  </Window.Triggers>
  <Rectangle x:Name="rect" Width="100" Height="100" Fill="WhiteSmoke" />
</Window>






     


Animate Width and Height of a Ellipse


   
     

<Window x:Class="Main"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="" Height="300" Width="300">
  <Window.Resources>
    <Storyboard x:Key="ellipse1Storyboard" Storyboard.TargetName="ellipse1">
      <ParallelTimeline>
        <DoubleAnimation To="50" Duration="0:0:5" AccelerationRatio="0.25" DecelerationRatio="0.25" Storyboard.TargetProperty="Width" RepeatBehavior="5x" />
        <DoubleAnimation To="50" Duration="0:0:5" AccelerationRatio="0.5" DecelerationRatio="0.25" Storyboard.TargetProperty="Height" RepeatBehavior="5x" SpeedRatio="4" />
      </ParallelTimeline>
    </Storyboard>
  </Window.Resources>
  <Window.Triggers>
    <EventTrigger RoutedEvent="Ellipse.Loaded" SourceName="ellipse1">
      <BeginStoryboard Storyboard="{DynamicResource ellipse1Storyboard}" />
    </EventTrigger>
  </Window.Triggers>
  <Grid>
    <Ellipse x:Name="ellipse1" Margin="10" Width="100" Height="100" Fill="CornflowerBlue" />
  </Grid>
</Window>






     


Animate Width and Height of a Rectangle


   
     

<Window x:Class="Main"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="" Height="300" Width="300">
  <Window.Resources>
    <Storyboard x:Key="rect1Storyboard" Storyboard.TargetName="rect1">
      <ParallelTimeline>
        <DoubleAnimation To="50" Duration="0:0:10" FillBehavior="Stop" Storyboard.TargetProperty="Width" />
        <DoubleAnimation To="50" Duration="0:0:5" FillBehavior="HoldEnd" AccelerationRatio="0.5" DecelerationRatio="0.25" Storyboard.TargetProperty="Height" />
      </ParallelTimeline>
    </Storyboard>
  </Window.Resources>
  <Window.Triggers>
    <EventTrigger RoutedEvent="Rectangle.Loaded" SourceName="rect1">
      <BeginStoryboard Storyboard="{StaticResource rect1Storyboard}" />
    </EventTrigger>
  </Window.Triggers>
  <Grid>
    <Rectangle x:Name="rect1" Margin="10" Width="100" Height="100" Fill="Firebrick" Grid.Column="1" />
  </Grid>
</Window>

   
    
    
    
    
     


Use ColorAnimationUsingKeyFrames to animate the foreground color of a TextBlock


   
      

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid TextBlock.FontSize="192">
        <TextBlock Name="txtblk" Foreground="Black"
                   HorizontalAlignment="Center" VerticalAlignment="Center" />
    </Grid> 
    <Page.Triggers>
        <EventTrigger RoutedEvent="Canvas.Loaded">
            <BeginStoryboard>
                <Storyboard TargetName="txtblk" Duration="0:0:2" RepeatBehavior="Forever">
                    <StringAnimationUsingKeyFrames Storyboard.TargetProperty="Text">
                        <DiscreteStringKeyFrame KeyTime="0:0:0" Value="textBoxA" />
                        <DiscreteStringKeyFrame KeyTime="0:0:1" Value="B" />
                    </StringAnimationUsingKeyFrames>

                    <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground.Color">
                        <DiscreteColorKeyFrame KeyTime="0:0:0" Value="Red" />
                        <DiscreteColorKeyFrame KeyTime="0:0:1" Value="Blue" />
                    </ColorAnimationUsingKeyFrames>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Page.Triggers>
</Page>

   
    
    
    
    
    
     


Use StringAnimationUsingKeyFrames to animate the text value of a TextBlock


   
      


<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid TextBlock.FontSize="192">
        <TextBlock Name="txtblk" Foreground="Black"
                   HorizontalAlignment="Center" VerticalAlignment="Center" />
    </Grid> 
    <Page.Triggers>
        <EventTrigger RoutedEvent="Canvas.Loaded">
            <BeginStoryboard>
                <Storyboard TargetName="txtblk" Duration="0:0:2" RepeatBehavior="Forever">
                    <StringAnimationUsingKeyFrames Storyboard.TargetProperty="Text">
                        <DiscreteStringKeyFrame KeyTime="0:0:0" Value="textBoxA" />
                        <DiscreteStringKeyFrame KeyTime="0:0:1" Value="B" />
                    </StringAnimationUsingKeyFrames>

                    <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground.Color">
                        <DiscreteColorKeyFrame KeyTime="0:0:0" Value="Red" />
                        <DiscreteColorKeyFrame KeyTime="0:0:1" Value="Blue" />
                    </ColorAnimationUsingKeyFrames>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Page.Triggers>
</Page>