DoubleAnimation Loop three times


   
  


<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="AnimatedLabel" Height="498" Width="530" WindowStartupLocation="CenterScreen" >
  <StackPanel>
    <Button Content ="Animate Height" Click ="btnAnimatelblMessage_Click" />
    <Label Name ="lblHeight"  Content ="Animate Height!"/>
    <Button Content ="Animate Transparency" Click ="btnAnimatelblTransparency_Click"/>
    <Label Background ="Cornsilk" Name ="lblTransparency"  Content ="Animate Transparency!"/>
  </StackPanel>
</Window>
//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;

namespace WpfApplication1
{
  public partial class MainWindow : Window
  {
    public MainWindow()
    {
      InitializeComponent();
    }

    protected void btnAnimatelblMessage_Click(object sender, RoutedEventArgs args)
    {
      DoubleAnimation dblAnim = new DoubleAnimation();
      dblAnim.From = 40;
      dblAnim.To = 200;

      dblAnim.AutoReverse = true;

      dblAnim.RepeatBehavior = new RepeatBehavior(3);


      dblAnim.Duration = new Duration(TimeSpan.FromSeconds(4));
      lblHeight.BeginAnimation(Label.HeightProperty, dblAnim);
    }

    protected void btnAnimatelblTransparency_Click(object sender, RoutedEventArgs args)
    {
      DoubleAnimation dblAnim = new DoubleAnimation();
      dblAnim.From = 1.0;
      dblAnim.To = 0.0;
      dblAnim.Duration = new Duration(TimeSpan.FromSeconds(10));
      lblTransparency.BeginAnimation(Label.OpacityProperty, dblAnim);
    }
  }
}

   
    
     


DoubleAnimation Loop forever


   
  


<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="AnimatedLabel" Height="498" Width="530" WindowStartupLocation="CenterScreen" >
  <StackPanel>
    <Button Content ="Animate Height" Click ="btnAnimatelblMessage_Click" />
    <Label Name ="lblHeight"  Content ="Animate Height!"/>
    <Button Content ="Animate Transparency" Click ="btnAnimatelblTransparency_Click"/>
    <Label Background ="Cornsilk" Name ="lblTransparency"  Content ="Animate Transparency!"/>
  </StackPanel>
</Window>
//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;

namespace WpfApplication1
{
  public partial class MainWindow : Window
  {
    public MainWindow()
    {
      InitializeComponent();
    }

    protected void btnAnimatelblMessage_Click(object sender, RoutedEventArgs args)
    {
      DoubleAnimation dblAnim = new DoubleAnimation();
      dblAnim.From = 40;
      dblAnim.To = 200;

      dblAnim.AutoReverse = true;

      dblAnim.RepeatBehavior = RepeatBehavior.Forever;


      dblAnim.Duration = new Duration(TimeSpan.FromSeconds(4));
      lblHeight.BeginAnimation(Label.HeightProperty, dblAnim);
    }

    protected void btnAnimatelblTransparency_Click(object sender, RoutedEventArgs args)
    {
      DoubleAnimation dblAnim = new DoubleAnimation();
      dblAnim.From = 1.0;
      dblAnim.To = 0.0;
      dblAnim.Duration = new Duration(TimeSpan.FromSeconds(10));
      lblTransparency.BeginAnimation(Label.OpacityProperty, dblAnim);
    }
  }
}

   
    
     


Create DoubleAnimation and Animate a Button with Button.BeginAnimation and Button.WidthProperty


   
  
<Window x:Class="Window1" Title="Animation" Width="300" Height="300"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Canvas>
    <Button x:Name="b">OK</Button>
  </Canvas>
</Window>

//File:Window.xaml.cs
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
        DoubleAnimation a = new DoubleAnimation();
        a.From = 50;
        a.To = 100;

        b.BeginAnimation(Button.WidthProperty, a);
    }
}

   
    
     


Animated transform


   
  

<Window x:Class="Workspace.DockExample"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Workspace" Width="640" Height="480">
  <StackPanel>

    
    
    <Canvas Width="700" Height="400" HorizontalAlignment="Center">
      <TextBlock Canvas.Top="100" Canvas.Left="50"
        TextAlignment="Center" FontSize="16pt" Foreground="#CCCCFF"
        RenderTransformOrigin="0.5,0.5">
        <TextBlock.RenderTransform>
          <RotateTransform x:Name="MyAnimatedRotateTransform" Angle="0" />
        </TextBlock.RenderTransform>
        RotateTransform
      </TextBlock>
    
      <TextBlock Canvas.Top="100" Canvas.Left="300" Name="scaledText"
        TextAlignment="Center" FontSize="16pt" Foreground="Gray"
        RenderTransformOrigin="0.5,0.5">
        <TextBlock.RenderTransform>
          <ScaleTransform x:Name="MyAnimatedScaleTransform" ScaleX="1" ScaleY="1" />
        </TextBlock.RenderTransform>
        ScaleTransform
      </TextBlock>    
    
      <TextBlock Canvas.Top="250" Canvas.Left="150" 
        TextAlignment="Center" FontSize="16pt" Foreground="Orange"
        RenderTransformOrigin="0.5,0.5">
        <TextBlock.RenderTransform>
          <SkewTransform x:Name="MyAnimatedSkewTransform" AngleX="0" AngleY="0" />
        </TextBlock.RenderTransform>
        SkewTransform
      </TextBlock>      
    
      <TextBlock Canvas.Top="30" Canvas.Left="300" Name="translatedText"
        FontSize="16pt">
        <TextBlock.RenderTransform>
          <TranslateTransform x:Name="MyAnimatedTranslateTransform" X="0" Y="0" />
        </TextBlock.RenderTransform>
        TranslateTransform
      </TextBlock>      
    
    </Canvas>
  </StackPanel>
  

  <Window.Triggers>
    <EventTrigger RoutedEvent="Page.Loaded">
      <BeginStoryboard>
        <Storyboard>
          <DoubleAnimation 
            Storyboard.TargetName="MyAnimatedRotateTransform" 
            Storyboard.TargetProperty="Angle" 
            From="0" To="360" Duration="0:0:5" RepeatBehavior="Forever" />
          <ParallelTimeline RepeatBehavior="Forever" AutoReverse="True">
            <DoubleAnimation 
              Storyboard.TargetName="MyAnimatedScaleTransform" 
              Storyboard.TargetProperty="ScaleX"
              From="0" To="3" Duration="0:0:7" />
            <DoubleAnimation 
              Storyboard.TargetName="MyAnimatedScaleTransform" 
              Storyboard.TargetProperty="ScaleY"          
              From="0" To="3" Duration="0:0:7" />     
          </ParallelTimeline>
          <ParallelTimeline RepeatBehavior="Forever" AutoReverse="True">
            <DoubleAnimation 
              Storyboard.TargetName="MyAnimatedSkewTransform" 
              Storyboard.TargetProperty="AngleX"
              From="-45" To="70" Duration="0:0:10" />
            <DoubleAnimation 
              Storyboard.TargetName="MyAnimatedSkewTransform" 
              Storyboard.TargetProperty="AngleY"          
              From="-45" To="70" Duration="0:0:10" />       
          </ParallelTimeline>     
          <ParallelTimeline RepeatBehavior="Forever" AutoReverse="True">
            <DoubleAnimation 
              Storyboard.TargetName="MyAnimatedTranslateTransform" 
              Storyboard.TargetProperty="X"          
              From="0" To="100" Duration="0:0:5" />
            <DoubleAnimation 
              Storyboard.TargetName="MyAnimatedTranslateTransform" 
              Storyboard.TargetProperty="Y"          
              From="0" To="200" Duration="0:0:5" />   
          </ParallelTimeline>       
        </Storyboard>
      </BeginStoryboard>
    </EventTrigger>
  </Window.Triggers>

</Window>

   
    
     


Remove aniamtion with RemoveStoryboard


   
  
<Window x:Class="Wpf1Application.Window1"
  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="Storyboard1">
      <ParallelTimeline>
        <DoubleAnimation x:Name="Animation1" Storyboard.TargetProperty="Width" From="140" To="50"
          AutoReverse="True" RepeatBehavior="Forever" />
        <DoubleAnimation Storyboard.TargetProperty="Opacity" To="0.5" AutoReverse="True" RepeatBehavior="Forever" />
      </ParallelTimeline>
    </Storyboard>
  </Window.Resources>

  <UniformGrid>
    <Button Margin="5" Content="Method 1">
      <Button.Triggers>
        <EventTrigger RoutedEvent="Button.Loaded">
          <BeginStoryboard Storyboard="{DynamicResource Storyboard1}" x:Name="BeginStoryboard1" />
        </EventTrigger>
        <EventTrigger RoutedEvent="Button.Click">
          <RemoveStoryboard BeginStoryboardName="BeginStoryboard1" />
        </EventTrigger>
      </Button.Triggers>
    </Button>
  </UniformGrid>
</Window>

   
    
     


Show the effect of each value of the Dock property by manipulating two Rectangle elements.


   
  

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      x:Class="WpfApplication1.Page1"
      WindowTitle="Docking Sample">
  <DockPanel Background="White">
    <StackPanel Orientation="Horizontal" DockPanel.Dock="Top" Margin="0,0,0,10">
      <Button Click="OnClick1" Background="LightCoral">Dock = "Left"</Button>
      <Button Click="OnClick2" Background="LightCoral">Dock = "Right"</Button>
      <Button Click="OnClick3" Background="LightCoral">Dock = "Top"</Button>
      <Button Click="OnClick4" Background="LightCoral">Dock = "Bottom"</Button>
      <Button Click="OnClick5" Background="LightSkyBlue" Foreground="White">Dock = "Left"</Button>
      <Button Click="OnClick6" Background="LightSkyBlue" Foreground="White">Dock = "Right"</Button>
      <Button Click="OnClick7" Background="LightSkyBlue" Foreground="White">Dock = "Top"</Button>
      <Button Click="OnClick8" Background="LightSkyBlue" Foreground="White">Dock = "Bottom"</Button>
      <Button Click="OnClick9" Background="White">LastChildDock="True"</Button>
      <Button Click="OnClick10" Background="White">LastChildDock="False"</Button>
    </StackPanel>

    <Border Background="LightGoldenRodYellow" BorderBrush="Black" BorderThickness="1">
      <DockPanel Name="AnotherDockPanel">
        <Rectangle Name="rect1" MinWidth="200" MinHeight="200" Stroke="Black" Fill="LightCoral" />
        <Rectangle Name="rect2" MinWidth="200" MinHeight="200" Stroke="Black" Fill="LightSkyBlue" />
      </DockPanel>
    </Border>
  </DockPanel>
</Page>

//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Documents;
using System.Windows.Navigation;

namespace WpfApplication1
{
    public partial class Page1 : Page
    {
        public void OnClick1(object sender, RoutedEventArgs e)
        {
            DockPanel.SetDock(rect1, Dock.Left);
        }
       
        public void OnClick2(object sender, RoutedEventArgs e)
        {
            DockPanel.SetDock(rect1, Dock.Right);
        }

        public void OnClick3(object sender, RoutedEventArgs e)
        {
            DockPanel.SetDock(rect1, Dock.Top);
        }

        public void OnClick4(object sender, RoutedEventArgs e)
        {
            DockPanel.SetDock(rect1, Dock.Bottom);
        }

        public void OnClick5(object sender, RoutedEventArgs e)
        {
            DockPanel.SetDock(rect2, Dock.Left);
        }

        public void OnClick6(object sender, RoutedEventArgs e)
        {
            DockPanel.SetDock(rect2, Dock.Right);
        }

        public void OnClick7(object sender, RoutedEventArgs e)
        {
            DockPanel.SetDock(rect2, Dock.Top);
        }

        public void OnClick8(object sender, RoutedEventArgs e)
        {
            DockPanel.SetDock(rect2, Dock.Bottom);
        }

        public void OnClick9(object sender, RoutedEventArgs e)
        {
            AnotherDockPanel.LastChildFill = true;
        }

        public void OnClick10(object sender, RoutedEventArgs e)
        {
            AnotherDockPanel.LastChildFill = false;
        }
    }
}

   
    
     


Docking left and right before top and bottom


   
             

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="Simple DockPanel"
      Width="250" Height="150">
<DockPanel>
  <Button DockPanel.Dock="Left">Left</Button>
  <Button DockPanel.Dock="Right">Right</Button>
  <Button DockPanel.Dock="Top">Top</Button>
  <Button DockPanel.Dock="Bottom">Bottom</Button>
  <Button>Fill</Button>
</DockPanel>
</Window>