如何在WPF中滑动按钮/文本框或任何控件?

时间:2011-02-14 05:09:58

标签: wpf

我想简单地为文本框设置动画,使其淡入并向左移动(或任何x / y位置)。我怎样才能做到这一点?

如果它在网格内也会很重要吗?

1 个答案:

答案 0 :(得分:3)

这是一个粗略的方法,我刚刚写的任何类型的UIElement淡出:

    public static void FadeIn(UIElement element, int xOffset, TimeSpan duration)
    {
        Transform tempTrans = element.RenderTransform;
        TranslateTransform trans = new TranslateTransform(xOffset, 0);
        TransformGroup group = new TransformGroup();
        if (tempTrans != null) group.Children.Add(tempTrans);
        group.Children.Add(trans);

        DoubleAnimation animTranslate = new DoubleAnimation(0, (Duration)duration);
        animTranslate.EasingFunction = new CubicEase() { EasingMode = EasingMode.EaseOut };
        DoubleAnimation animFadeIn = new DoubleAnimation(0, 1, (Duration)duration) { FillBehavior = FillBehavior.Stop };
        animTranslate.Completed += delegate
        {
            element.RenderTransform = tempTrans;
        };

        element.RenderTransform = trans;
        element.BeginAnimation(UIElement.OpacityProperty, animFadeIn);
        trans.BeginAnimation(TranslateTransform.XProperty, animTranslate);
    }

如果某些工作不明确,请随时提出。