wpf - StopStoryboard 不会...停止 BeginStoryboard

标签 wpf xaml animation storyboard

我从我的项目中提取了这段代码,因为我试图找到我犯的一个错误,该错误使我的 BeginStoryboard 无法自行停止。我尽可能地简化了代码,但仍然没有发现问题。你认为它可能是什么?

<Window Width="640" Height="480" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<StackPanel>
    <Button Content="Start" Name="Button" Width="200">
        <Button.Triggers>
            <EventTrigger RoutedEvent="Button.Click">
                <BeginStoryboard Name="Storyboard">
                    <Storyboard>
                        <DoubleAnimation By="150" Duration="0:0:5" Storyboard.TargetName="Button" Storyboard.TargetProperty="Width"/>
                        <StringAnimationUsingKeyFrames Storyboard.TargetName="Button" Storyboard.TargetProperty="Content">
                            <DiscreteStringKeyFrame KeyTime="0:0:5" Value="Did you click? Because I obviously didn't stop..."/>
                        </StringAnimationUsingKeyFrames>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </Button.Triggers>
    </Button>
    <Button Content="Stop">
        <Button.Triggers>
            <EventTrigger RoutedEvent="Button.Click">
                <StopStoryboard BeginStoryboardName="Storyboard"/>
            </EventTrigger>
        </Button.Triggers>
    </Button>
</StackPanel>
</Window>

自己尝试一下代码,第一个按钮触发 Storyboard,第二个按钮应该停止它,但没有任何反应,所以第一个按钮中的动画顺利进行。

最佳答案

您的 BeginStoryboard 位于不同的命名范围内,因此 StopStoryboard 看不到它。

您需要将两个触发器放在同一个集合中,例如 the MSDN example .

关于wpf - StopStoryboard 不会...停止 BeginStoryboard,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7930397/

相关文章:

c# - 使用框架的 WPF 的 MVVM Light Toolkit NavigationService 实现

c# - 如何制作可变大小的 GridView 项目?

image - 扑克牌翻转动画

ios - InsertRow 动画将 UITableView 向上滚动

java - 动画岩石无法正确生成

wpf - 如何在 WPF 中动态创建标签和文本框列表?

c# - 在 c++/cli 中包装 Interop HwndSource 以在 native c++/MFC 应用程序中使用

wpf - 如何强制 WPF 应用程序在管理员模式下运行

c# - 将多个资源字典动态添加到我的 WPF 项目

WPF 轮播/旋转元素