在 wpf 中为按钮的位置设置动画最简单的方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16652184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:49:48  来源:igfitidea点击:

What is the easiest to animate the position of button in wpf?

wpfanimationwpf-controls

提问by Hmitchang

I want to move a button across a canvas when it's clicked in a way that it changes its position gradually(animation), not vanishing and reappearing in the new position.

当一个按钮被点击时,我想在画布上移动一个按钮,它会逐渐改变它的位置(动画),而不是消失并重新出现在新位置。

回答by Sonhja

You can use a Storyboardthat will move it smoothly:

您可以使用一个Storyboard可以平稳移动它的方法:

    <Grid>
    <Canvas>
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="50,250,0,0" Name="buttonAnimate" VerticalAlignment="Top" Width="75">
            <Button.Triggers>
                <EventTrigger RoutedEvent="Button.Click">
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation From="0" To="100" Duration="0:0:2" Storyboard.TargetProperty="(Canvas.Left)" AutoReverse="False" />
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Button.Triggers>    
        </Button>
    </Canvas>
</Grid>