0

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.

Osama Kawish
  • 312
  • 1
  • 5
  • 15
  • 1
    Well from the comment in this question (https://stackoverflow.com/q/9048226/5869304), I would guess you would need to write `new PropertyPath("Canvas.TopProperty")` (note the quotation marks added). Or something like that – Joe Jan 15 '23 at 00:19
  • @Joe That turned out to be correct. I was also missing the fact that the first parameter had to be the animation variable, not the rectangle. Edit: Actually even the code as-is with the first parameter being the animation resolved the issue. – Osama Kawish Jan 15 '23 at 00:39

0 Answers0