简体   繁体   English

从同一个点击事件中调用两个不同的动画

[英]Calling two different animations from the same click event

I'm currently working on a Surface application where I need to call two different animations when a button is tapped. 我正在使用Surface应用程序,我需要在点击按钮时调用两个不同的动画。

How exactly should I be doing this? 我该怎么做呢? I'd like to do it declaratively if it's possible. 如果有可能,我想以声明的方式做。 Should I be using MultiTriggers for this, or? 我应该使用MultiTriggers,还是?

Thanks in advance! 提前致谢!

You can do this with an EventTrigger. 您可以使用EventTrigger执行此操作。

You can define the trigger in a FrameworkElement.Triggers property of any container of both the button and the animation targets. 您可以在按钮和动画目标的任何容器的FrameworkElement.Triggers属性中定义触发器。

    <StackPanel
        Orientation="Horizontal">

        <StackPanel.Triggers>

            <EventTrigger
                SourceName="TheButton"
                RoutedEvent="Button.Click">

                <BeginStoryboard>
                    <Storyboard>
                        <ColorAnimation
                            Storyboard.TargetName="LimeRect"
                            Storyboard.TargetProperty="Fill.Color"
                            To="Red" />
                        <ColorAnimation
                            Storyboard.TargetName="RedRect"
                            Storyboard.TargetProperty="Fill.Color"
                            To="Lime" />
                    </Storyboard>
                </BeginStoryboard>

            </EventTrigger>

        </StackPanel.Triggers>


        <Button
            x:Name="TheButton"
            Content="Play" />

        <Rectangle
            x:Name="LimeRect"
            Fill="Lime"
            Width="50"
            Height="50" />

        <Rectangle
            x:Name="RedRect"
            Fill="Red"
            Width="50"
            Height="50" />

    </StackPanel>

If there is a relative path to your targets, you can use Storyboard.Target="{Binding PathToTarget}" in place of Storyboard.TargetName="TargetName" . 如果目标有相对路径,则可以使用Storyboard.Target="{Binding PathToTarget}"代替Storyboard.TargetName="TargetName"

EDIT: (see comments) 编辑:(见评论)

If you are animating the button itself, you can put the triggers right in the button and you don't need any target names. 如果您要为按钮本身设置动画,则可以将触发器放在按钮中,而不需要任何目标名称。

Example - Animating the size of a ToggleButton: 示例 - 动画ToggleButton的大小:

    <ToggleButton
        Content="Toggle"
        Width="50"
        Height="50">

        <ToggleButton.Triggers>

            <EventTrigger
                RoutedEvent="ToggleButton.Checked">
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation
                            Duration="00:00:00.2"
                            Storyboard.TargetProperty="Width"
                            To="100" />
                        <DoubleAnimation
                            Duration="00:00:00.2"
                            Storyboard.TargetProperty="Height"
                            To="100" />
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>

            <EventTrigger
                RoutedEvent="ToggleButton.Unchecked">
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation
                            Duration="00:00:00.2"
                            Storyboard.TargetProperty="Width"
                            To="50" />
                        <DoubleAnimation
                            Duration="00:00:00.2"
                            Storyboard.TargetProperty="Height"
                            To="50" />
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>

        </ToggleButton.Triggers>

    </ToggleButton>

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

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