Here, _storyboard
is just a private Storyboard
member of MainWindow
.
// Create rectangle on canvas.
var rect = new Rectangle
{
Name = "Rect",
Width = 200,
Height = 80,
Fill = Brushes.Brown
};
RegisterName(rect.Name, rect);
MainCanvas.Children.Add(rect);
Canvas.SetLeft(rect, 110); Canvas.SetTop(rect, 43);
// Add animation to the storyboard
var doubleAnim = new DoubleAnimation(87, 166d, new Duration(TimeSpan.FromSeconds(10)));
_storyboard.Children.Add(doubleAnim);
Storyboard.SetTarget(doubleAnim, rect);
Storyboard.SetTargetProperty(rect, new PropertyPath(Canvas.TopProperty));
rect.Loaded += (_, _) => _storyboard.Begin(this);
I'm getting the following error on the last line (rect.Loaded += (_, _) => _storyboard.Begin(this);
):
System.InvalidOperationException: 'Must specify TargetProperty for 'System.Windows.Media.Animation.DoubleAnimation'.'
However, I'm not sure how else to add the top property to the rectangle.