【发布时间】:2017-03-08 13:36:03
【问题描述】:
我有一个自定义按钮。出于某种原因,我想在后面的代码中应用转换。并且转换工作正常。但是当我尝试在变换上添加动画时,动画不会生效。我的代码如下。
public partial class MyButton : Button
{
private TranslateTransform translate = new TranslateTransform() { X = 200, Y = 100 };
public MyButton()
{
InitializeComponent();
this.RenderTransform = new TransformGroup()
{
Children =
{
this.translate
}
};
}
protected override void OnClick()
{
base.OnClick();
// Edit: I need to use Storyboard to synchronized animation1 and animation2.
var sb = new Storyboard();
var animation1 = new DoubleAnimation(200, -10, new Duration(new TimeSpan(0, 0, 0, 1, 0)));
Storyboard.SetTarget(animation1, this.translate);
Storyboard.SetTargetProperty(animation1, new PropertyPath("X"));
sb.Children.Add(animation1);
var animation2 = new DoubleAnimation(100, 0, new Duration(new TimeSpan(0, 0, 0, 1, 0)));
Storyboard.SetTarget(animation2, this.translate);
Storyboard.SetTargetProperty(animation2, new PropertyPath("Y"));
sb.Children.Add(animation2);
sb.Begin(this);
}
}
谁能解释一下为什么以及如何为这个场景添加动画?
【问题讨论】:
标签: wpf wpf-controls