简体   繁体   English

单击 c# wpf 时如何旋转 animation 我的按钮

[英]How to rotate animation my button when click in c# wpf

these are my declaration for my DoubleAnimation and storyboard这些是我对 DoubleAnimation 和 storyboard 的声明

private Storyboard openmenurotateSB;

DoubleAnimation openMenuRotate = new DoubleAnimation();
openMenuRotate.From = 0;
openMenuRotate.To = 90;
openMenuRotate.Duration = new Duration(TimeSpan.FromMilliseconds(1000));
openMenuRotate.FillBehavior = FillBehavior.Stop;
openMenuRotate.AutoReverse = true;
openmenurotateSB = new Storyboard();
openmenurotateSB.Children.Add(openMenuRotate);
Storyboard.SetTargetName(openMenuRotate, OpenMenuButton.Name);
Storyboard.SetTargetProperty(openMenuRotate, new PropertyPath(RotateTransform.AngleProperty));

when click, begin rotate animation单击时,开始旋转 animation

private void OpenMenu_Click(object sender, RoutedEventArgs e)
        {
            openmenurotateSB.Begin(OpenMenuButton);
        }

There is no any error or exception, it just does not work.没有任何错误或异常,它只是不起作用。

Thanks in advance.提前致谢。

Xing Yik兴益

A RotateTransform should be assigned to either the RenderTransform or the LayoutTransform property of the Button. RotateTransform应分配给 Button 的RenderTransformLayoutTransform属性。

<Button Content="Open Menu" Click="OpenMenu_Click"
        RenderTransformOrigin="0.5,0.5">
    <Button.RenderTransform>
        <RotateTransform x:Name="rotateTransform"/>
    </Button.RenderTransform>
</Button>

You do not need to use a Storyboard.您不需要使用 Storyboard。 Just directly animate the RotateTransform.只需直接为 RotateTransform 设置动画即可。

private void OpenMenu_Click(object sender, RoutedEventArgs e)
{
    var animation = new DoubleAnimation
    {
        To = 90,
        Duration = TimeSpan.FromSeconds(1),
        FillBehavior = FillBehavior.Stop,
        AutoReverse = true
    };

    rotateTransform.BeginAnimation(
        RotateTransform.AngleProperty, animation);
}

or if you don't want to assign an x:Name to the RotateTransform or have assigned the RenderTransform property in code behind:或者如果您不想将x:Name分配给 RotateTransform 或在后面的代码中分配了 RenderTransform 属性:

((UIElement)sender).RenderTransform.BeginAnimation(
    RotateTransform.AngleProperty, animation);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM