c# - 在代码问题中应用动画 ScaleTransform

标签 c# wpf animation rendertransform

我试图找出为什么下面的代码似乎不起作用。它不会给出错误 - 它根本无法扩展。如果我将它更改为我的第二个代码示例,它实际上似乎确实有效。 有人知道吗?

谢谢

public static void StartMouseEnterAnimation(Button button)
    {
        Storyboard storyboard = new Storyboard();

        ScaleTransform scale = new ScaleTransform(1.0, 1.0, 1, 1);
        button.RenderTransformOrigin = new Point(0.5, 0.5);
        button.RenderTransform = scale;

        DoubleAnimation growAnimation = new DoubleAnimation();
        growAnimation.Duration = TimeSpan.FromMilliseconds(300);
        growAnimation.From = 1;
        growAnimation.To = 1.8;
        storyboard.Children.Add(growAnimation);

        Storyboard.SetTargetProperty(growAnimation, new PropertyPath(ScaleTransform.ScaleXProperty));
        Storyboard.SetTarget(growAnimation, scale);

        storyboard.Begin();
    }

--- 以下确实有效,但我必须创建一个 TransformGroup 并通过更复杂的 PropertyChain 引用它...

public static void StartMouseEnterAnimation(Button button)
    {    
        Storyboard storyboard = new Storyboard();            
        ScaleTransform scale = new ScaleTransform(1.0, 1.0, 1, 1);
        button.RenderTransformOrigin = new Point(0.5, 0.5);
        TransformGroup myTransGroup = new TransformGroup();
        myTransGroup.Children.Add(scale);
        button.RenderTransform = myTransGroup;

        DoubleAnimation growAnimation = new DoubleAnimation();
        growAnimation.Duration = TimeSpan.FromMilliseconds(100);
        //growAnimation.From = 1;
        growAnimation.To = 1.1;
        storyboard.Children.Add(growAnimation);

        DependencyProperty[] propertyChain = new DependencyProperty[]
        {
            Button.RenderTransformProperty, 
            TransformGroup.ChildrenProperty,
            ScaleTransform.ScaleXProperty
        };
        string thePath = "(0).(1)[0].(2)";
        PropertyPath myPropertyPath = new PropertyPath(thePath, propertyChain);
        Storyboard.SetTargetProperty(growAnimation, myPropertyPath);
        Storyboard.SetTarget(growAnimation, button);

        storyboard.Begin();
   }

最佳答案

通过像这样调整您的第一个代码示例,我能够让它工作:

public static void StartMouseEnterAnimation(Button button) {
    Storyboard storyboard = new Storyboard();

    ScaleTransform scale = new ScaleTransform(1.0, 1.0);
    button.RenderTransformOrigin = new Point(0.5, 0.5);
    button.RenderTransform = scale;

    DoubleAnimation growAnimation = new DoubleAnimation();
    growAnimation.Duration = TimeSpan.FromMilliseconds(300);
    growAnimation.From = 1;
    growAnimation.To = 1.8;
    storyboard.Children.Add(growAnimation);

    Storyboard.SetTargetProperty(growAnimation, new PropertyPath("RenderTransform.ScaleX"));
    Storyboard.SetTarget(growAnimation, button);

    storyboard.Begin();
}

我使用的不是 new PropertyPath(ScaleTransform.ScaleXProperty)),而是 new PropertyPath("RenderTransform.ScaleX")),我将 Storyboard的目标设置为按钮(不是 scaleTransform 本身)。

希望对您有所帮助!

关于c# - 在代码问题中应用动画 ScaleTransform,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2131797/

相关文章:

c# - WPF 数据变化动画

ios - 自定义 Segue 类 UIView 动画问题

ios - UIView +animateWithDuration : isn't animating a recently allocated view correctly

c# - 在 C# 中使用按钮清除多个文本框

c# - LINQ - 字符串包含数组中的任何元素

c# - WPF MediaElement 应用的播放列表

html - 从当前位置(未知)到底部 : 0% 的 css 动画

c# - 具有 IMEX 1 的 OleDBConnection 忽略时间值中的 AM/PM

c# - 当鼠标悬停在按钮WPF上时,如何制作更改图像的图像按钮?

c# - 添加新项目后立即添加新行