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>






     


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>






     


Animation with Mixed Key Frames.xaml


   
     
<Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
  <Image Source="c:image.png">
    <Image.Triggers>
      <EventTrigger RoutedEvent="Image.Loaded">
        <EventTrigger.Actions>
          <BeginStoryboard>
            <Storyboard>
              <DoubleAnimation Storyboard.TargetProperty="(Canvas.Left)" From="0" To="500" Duration="0:0:3"/>
              <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(Canvas.Top)" Duration="0:0:3">
                  <DiscreteDoubleKeyFrame Value="0" KeyTime="0:0:0"/>
                  <LinearDoubleKeyFrame Value="200" KeyTime="0:0:1"/>
                  <SplineDoubleKeyFrame KeySpline="0,1,1,0" Value="200" KeyTime="0:0:3"/>
              </DoubleAnimationUsingKeyFrames>
            </Storyboard>
          </BeginStoryboard>
        </EventTrigger.Actions>
      </EventTrigger>
    </Image.Triggers>
  </Image>
</Canvas>






     


Animation with Discrete Key Frames


   
     

<Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
  <Image Source="c:image.png">
    <Image.Triggers>
      <EventTrigger RoutedEvent="Image.Loaded">
        <EventTrigger.Actions>
          <BeginStoryboard>
            <Storyboard>
              <DoubleAnimation Storyboard.TargetProperty="(Canvas.Left)" From="0" To="500" Duration="0:0:3"/>
              <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(Canvas.Top)" Duration="0:0:3">
                <DiscreteDoubleKeyFrame Value="0" KeyTime="0:0:0"/>
                <DiscreteDoubleKeyFrame Value="200" KeyTime="0:0:1"/>
                <DiscreteDoubleKeyFrame Value="0" KeyTime="0:0:2"/>
              </DoubleAnimationUsingKeyFrames>
            </Storyboard>
          </BeginStoryboard>
        </EventTrigger.Actions>
      </EventTrigger>
    </Image.Triggers>
  </Image>
</Canvas>






     


XAML Only Animation



     


<Window x:Class="XamlOnly"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    
xmlns:x=
"http://schemas.microsoft.com/winfx/2006/xaml" 
Title=
"XamlOnly"

    Height="300" Width="300">

    <Grid>

      <StackPanel>

            <TextBlock Name="textBlock" Margin="5"

                TextAlignment="Center" Height="30"

                Text="{Binding ElementName=textBox,Path=Text}" />

            <TextBox Name="textBox" Margin="5" Width="200"

                TextAlignment="Center" Text="Hello, WPF!" />

            <Button Margin="5" Width="200"

                Content="Change Text Color">

                <Button.Triggers>

                    <EventTrigger RoutedEvent="Button.Click">

                        <BeginStoryboard>

                            <Storyboard>

                                <ColorAnimation

                                    Storyboard.TargetName="textBlock"
                                    Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)"
                                    From="Black" To="Red" Duration="0:0:1" />
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                </Button.Triggers>
            </Button>
        </StackPanel>
   </Grid>
</Window>

  

Animate Ellipse RadiusY, RadiusX


   
     

<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">
  <Grid>
    <Path Stroke="Black" StrokeThickness="1">
      <Path.Data>
        <GeometryGroup>
          <EllipseGeometry x:Name="ellipse" Center="150,150" RadiusX="5" RadiusY="5" />
        </GeometryGroup>
      </Path.Data>
      <Path.Triggers>
        <EventTrigger RoutedEvent="Path.Loaded">
          <BeginStoryboard>
            <Storyboard AutoReverse="True" RepeatBehavior="Forever">
              <ParallelTimeline Storyboard.TargetName="ellipse">
                <DoubleAnimation To="80" Storyboard.TargetProperty="RadiusX" />
                <DoubleAnimation To="80" Storyboard.TargetProperty="RadiusY" />
              </ParallelTimeline>
            </Storyboard>
          </BeginStoryboard>
        </EventTrigger>
      </Path.Triggers>
    </Path>
  </Grid>
</Window>






     


Animate StartPoint

       

<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">
  <Grid>
    <Path Stroke="Black" StrokeThickness="1">
      <Path.Data>
        <GeometryGroup>
          <LineGeometry x:Name="line2" StartPoint="38,40" EndPoint="248,40" />
        </GeometryGroup>
      </Path.Data>
      <Path.Triggers>
        <EventTrigger RoutedEvent="Path.Loaded">
          <BeginStoryboard>
            <Storyboard AutoReverse="True" RepeatBehavior="Forever">
              <PointAnimation To="280,40" Storyboard.TargetName="line2" Storyboard.TargetProperty="StartPoint" />
            </Storyboard>
          </BeginStoryboard>
        </EventTrigger>
      </Path.Triggers>
    </Path>
  </Grid>
</Window>