WPF翻译矩形

时间:2015-08-02 07:46:01

标签: c# wpf

private void Window_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
    DoubleAnimation rightAnimation = new DoubleAnimation();

    rightAnimation.Duration = TimeSpan.FromSeconds(2);
    rightAnimation.From = 0;
    rightAnimation.To = 200;

    Storyboard.SetTarget(rightAnimation , rect1);

    Storyboard.SetTargetProperty(rightAnimation, new PropertyPath("(0).(1)", new DependencyProperty[]{UIElement.RenderTransformProperty, TranslateTransform.XProperty}));
    Storyboard sb = new Storyboard();
    sb.AccelerationRatio = 0.8;

    sb.Children.Add(rightAnimation);

    sb.Begin();

}

我想将rect1加速到右边..当我运行它时没有任何反应。

1 个答案:

答案 0 :(得分:1)

您可以使用TransformGroup为所有转换类型设置动画。

        DoubleAnimation rightAnimation = new DoubleAnimation();

        rightAnimation.Duration = TimeSpan.FromSeconds(2);

        rightAnimation.From = 0;

        rightAnimation.To = 200;

        TransformGroup transGroup = new TransformGroup();
        transGroup.Children.Add(new TranslateTransform());
        rect1.RenderTransform = transGroup;

        Storyboard.SetTarget(rightAnimation, rect1);
        Storyboard.SetTargetProperty(rightAnimation, new PropertyPath("RenderTransform.Children[0].X"));
        Storyboard sb = new Storyboard();
        sb.AccelerationRatio = 0.8;
        sb.Children.Add(rightAnimation);
        sb.Begin();