It seems that the documentation requires the storyboard to be registered as a Resource in order to function properly, that means applying a name to it, and adding it as a Resource etc. This is quite well explained in
MSDN[
^], but I didnt know about it either.
Implementation taken from a question on StackOverFlow seem to give you the answer on how to proceed:
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace WpfAnnimationTransform
{
public partial class MainWindow : Window
{
Canvas panel;
public MainWindow()
{
InitializeComponent();
MouseDown += DoDynamicAnimation;
Content = panel = new Canvas();
}
void DoDynamicAnimation(object sender, MouseButtonEventArgs args)
{
for (int i = 0; i < 12; ++i)
{
var e = new Button { Width = 16, Height = 16 };
Canvas.SetLeft(e, Mouse.GetPosition(this).X);
Canvas.SetTop(e, Mouse.GetPosition(this).Y);
var tg = new TransformGroup();
var translation = new TranslateTransform(30, 0);
var translationName = "myTranslation" + translation.GetHashCode();
RegisterName(translationName, translation);
tg.Children.Add(translation);
tg.Children.Add(new RotateTransform(i * 30));
e.RenderTransform = tg;
panel.Children.Add(e);
var anim = new DoubleAnimation(3, 150, new Duration(new TimeSpan(0, 0, 0, 2, 0)))
{
EasingFunction = new PowerEase { EasingMode = EasingMode.EaseOut }
};
var s = new Storyboard();
Storyboard.SetTargetName(s, translationName);
Storyboard.SetTargetProperty(s, new PropertyPath(TranslateTransform.YProperty));
var storyboardName = "s" + s.GetHashCode();
Resources.Add(storyboardName, s);
s.Children.Add(anim);
s.Completed +=
(sndr, evtArgs) =>
{
panel.Children.Remove(e);
Resources.Remove(storyboardName);
UnregisterName(translationName);
};
s.Begin();
}
}
}
}
The code is taken from
this[
^]. A little too many buttons perhaps ;)