Controlling Animations Programmatically
<UserControl x:Class='SilverlightApplication3.MainPage'
xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
xmlns:d='http://schemas.microsoft.com/expression/blend/2008'
xmlns:mc='http://schemas.openxmlformats.org/markup-compatibility/2006'
mc:Ignorable='d'
d:DesignWidth='640'
d:DesignHeight='480'>
<UserControl.Resources>
<Storyboard x:Name="FadeIt">
<DoubleAnimation Storyboard.TargetName="btnFade"
Storyboard.TargetProperty="(UIElement.Opacity)"
From=".25" To="1" Duration="00:00:01"/>
</Storyboard>
<Storyboard x:Name="SizeIt">
<DoubleAnimation Storyboard.TargetName="btnSize"
Storyboard.TargetProperty="(UIElement.RenderTransform).
(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"
From="1" To="2" Duration="00:00:01"/>
<DoubleAnimation Storyboard.TargetName="btnSize"
Storyboard.TargetProperty="(UIElement.RenderTransform).
(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"
From="1" To="2" Duration="00:00:01"/>
</Storyboard>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="Black">
<Button x:Name="btnSize" Content="Resize">
<Button.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Button.RenderTransform>
</Button>
<Button x:Name="btnFade" Content="Fade In"
Height="50" Width="150" Margin="100,120,0,0"
HorizontalAlignment="Left" VerticalAlignment="Top"
Opacity="0.25"/>
</Grid>
</UserControl>
//File: Page.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
namespace SilverlightApplication3
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
btnFade.MouseEnter += new MouseEventHandler(btnFade_MouseEnter);
btnSize.MouseEnter += new MouseEventHandler(btnSize_MouseEnter);
btnFade.MouseLeave += new MouseEventHandler(btnFade_MouseLeave);
btnSize.MouseLeave += new MouseEventHandler(btnSize_MouseLeave);
}
void btnSize_MouseEnter(object sender, MouseEventArgs e)
{
SizeIt.Begin();
}
void btnFade_MouseEnter(object sender, MouseEventArgs e)
{
FadeIt.Begin();
}
void btnSize_MouseLeave(object sender, MouseEventArgs e)
{
SizeIt.Stop();
}
void btnFade_MouseLeave(object sender, MouseEventArgs e)
{
FadeIt.Stop();
}
}
}
Related examples in the same category