DoubleAnimation multi times (C# 코드로 DoubleAnimation 처리)
WPF 2007. 9. 20. 03:32 |<Window x:Class="WpfApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow">
<Canvas>
<Button x:Name="MyButton" Loaded="MyButton_Loaded">Button</Button>
</Canvas>
</Window>
private void MyButton_Loaded(object sender, RoutedEventArgs e)
{
const string TranslateTransformName = "MyAnimatedTranslateTransform";
TranslateTransform tt = new TranslateTransform(50, 50);
NameScope.SetNameScope(tt, new NameScope());
this.RegisterName(TranslateTransformName, tt);
this.MyButton.RenderTransform = tt;
Storyboard sb = new Storyboard();
DoubleAnimation daX = new DoubleAnimation(50, 200, new Duration(new TimeSpan(0, 0, 5)));
daX.AutoReverse = true;
daX.RepeatBehavior = RepeatBehavior.Forever;
Storyboard.SetTargetName(daX, TranslateTransformName);
Storyboard.SetTargetProperty(daX, new PropertyPath(TranslateTransform.XProperty));
sb.Children.Add(daX);
DoubleAnimation daY = new DoubleAnimation(50, 200, new Duration(new TimeSpan(0, 0, 5)));
daY.AutoReverse = true;
daY.RepeatBehavior = RepeatBehavior.Forever;
Storyboard.SetTargetName(daY, TranslateTransformName);
Storyboard.SetTargetProperty(daY, new PropertyPath(TranslateTransform.YProperty));
sb.Children.Add(daY);
DoubleAnimation daW = new DoubleAnimation(50, 200, new Duration(new TimeSpan(0, 0, 5)));
daW.AutoReverse = true;
daW.RepeatBehavior = RepeatBehavior.Forever;
Storyboard.SetTargetName(daW, "MyButton");
Storyboard.SetTargetProperty(daW, new PropertyPath(Button.WidthProperty));
sb.Children.Add(daW);
DoubleAnimation daH = new DoubleAnimation(50, 200, new Duration(new TimeSpan(0, 0, 5)));
daH.AutoReverse = true;
daH.RepeatBehavior = RepeatBehavior.Forever;
Storyboard.SetTargetName(daH, "MyButton");
Storyboard.SetTargetProperty(daH, new PropertyPath(Button.HeightProperty));
sb.Children.Add(daH);
sb.Begin(this.MyButton);
}