0

我正在尝试移动一个矩形。有没有其他的属性可以使用。请帮帮我。谢谢你!

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();
4

1 回答 1

1

这是因为SetLeftSetTop是方法而不是属性。你需要做的是:

Storyboard.SetTargetProperty(myDoubleAnimation1, new PropertyPath(Canvas.LeftProperty));
Storyboard.SetTargetProperty(myDoubleAnimation2, new PropertyPath(Canvas.TopProperty));
于 2013-05-24T09:19:32.300 回答