Reputation: 13
I am trying to move a rectangle. Is there some other property to be used. Please help me out. Thank You!
Rectangle myRectangle = new Rectangle();
Color myColor = Color.FromArgb(255, 255, 0, 0);
SolidColorBrush myBrush = new SolidColorBrush();
myRectangle.Width = 200;
myRectangle.Height = 200;
myBrush.Color = myColor;
myRectangle.Fill = myBrush;
Canvas.SetLeft(myRectangle, 200);
Canvas.SetTop(myRectangle, 200);
canvasPanel.Children.Add(myRectangle);
Duration duration = new Duration(TimeSpan.FromSeconds(2));
DoubleAnimation myDoubleAnimation1 = new DoubleAnimation();
DoubleAnimation myDoubleAnimation2 = new DoubleAnimation();
myDoubleAnimation1.Duration = duration;
myDoubleAnimation2.Duration = duration;
Storyboard sb = new Storyboard();
sb.Duration = duration;
sb.Children.Add(myDoubleAnimation1);
sb.Children.Add(myDoubleAnimation2);
Storyboard.SetTarget(myDoubleAnimation1, myRectangle);
Storyboard.SetTarget(myDoubleAnimation2, myRectangle);
Storyboard.SetTargetProperty(myDoubleAnimation1, new PropertyPath("Canvas.SetLeft"));
Storyboard.SetTargetProperty(myDoubleAnimation2, new PropertyPath("Canvas.SetTop"));
myDoubleAnimation1.To = 200;
myDoubleAnimation2.To = 200;
canvasPanel.Resources.Add("unique_id", sb);
RootWindow.Content = canvasPanel;
sb.Begin();
Upvotes: 0
Views: 443
Reputation: 33364
It's because SetLeft
and SetTop
are methods not properties. What you need to do instead is:
Storyboard.SetTargetProperty(myDoubleAnimation1, new PropertyPath(Canvas.LeftProperty));
Storyboard.SetTargetProperty(myDoubleAnimation2, new PropertyPath(Canvas.TopProperty));
Upvotes: 1