Simple Animation In WPF

Abhisek
Posted by Abhisek under WPF category on | Views : 3554
<Window x:Class="MyWPFApp.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Simple Animation" Height="192" Width="428">

<!-- Do everything in a stack panel-->
<StackPanel Margin="20">

<!-- Create a rectangle to which will be animated -->
<Rectangle Name="SimpleGreenRectangle"
Fill="Firebrick" Width="50" Height="26">
<Rectangle.BitmapEffect>
<OuterGlowBitmapEffect GlowColor="DarkViolet" />
</Rectangle.BitmapEffect>
</Rectangle>
<!-- Create a second stack panel to hold
buttons to start and stop the animations -->
<StackPanel Orientation="Vertical" Margin="0,20,0,0">
<Button Name="btnStart" Width="133">
<Button.BitmapEffect>
<DropShadowBitmapEffect Color="Chartreuse" />
</Button.BitmapEffect> Start</Button>
<Button Name="btnStop" Width="132">
<Button.BitmapEffect>
<DropShadowBitmapEffect Color="Chartreuse" />
</Button.BitmapEffect> Stop</Button>

<!-- Associating the clicking of each button
with a stack of XAML that starts or stops the animation -->
<StackPanel.Triggers>
<EventTrigger SourceName="btnStart" RoutedEvent="Button.Click">
<BeginStoryboard Name="myStoryboard">
<Storyboard>
<!-- Create an animation that repeats indefinetly. -->
<DoubleAnimation
Storyboard.TargetName="SimpleGreenRectangle"
Storyboard.TargetProperty="Width"
From="50" To="300" Duration="0:0:4" RepeatBehavior="Forever" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger SourceName="btnStop" RoutedEvent="Button.Click">
<StopStoryboard BeginStoryboardName="myStoryboard" />
</EventTrigger>
</StackPanel.Triggers>

</StackPanel>
</StackPanel>
</Window>

Comment InitializeComponent(); in the .cs file.

Comments or Responses

Login to post response