2

I only have experience with C/C++ and just moved to C# and WPF. I want to create an animation to move a component(e.g an image), but I don't know why the following is illegal:

ThicknessAnimation a = new ThicknessAnimation(...);

Image1.BeginAnimation(Image1.Margin, a); // illegal. Image.Margin illegal too

It seems just can't use Margin here. Of course create a timer and create Thickness objects for Margin manually can work, but that will be dull and if Animation is possible, it will be more elegant.

Is a storyboard required here? I heard some says create a storyboard and you can use Margin property, but I don't know storyboard at all and can't understand that. Thanks

4

1 回答 1

3

您会为依赖属性设置动画,而不是常规属性,因此请尝试使用 Image.MarginProperty。

这些是您所定位的类型或基本类型中的静态字段。

public static readonly DependencyProperty MarginProperty

在您的情况下,它是在 FrameworkElement 中定义的。

参考 :

http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.marginproperty.aspx

http://msdn.microsoft.com/en-us/library/ms590761.aspx

对于更复杂的动画,您需要一个故事板:

Storyboard 对象使您能够将影响各种对象和属性的时间线组合到单个时间线树中,从而轻松组织和控制复杂的时序行为。

http://msdn.microsoft.com/en-us/library/ms742868.aspx

编辑 :

查看 Intellisense 时,您可以看到相关的依赖属性位于常规属性的下方。

在此处输入图像描述

我之前提到您必须使用 Image.MarginProperty 但实际上您可以忽略“图像”。部分作为对象确实已经从该基本类型继承,例如动画 'this' :

在此处输入图像描述

于 2013-06-14T04:07:52.507 回答