如何停止带有数据触发器的自定义故事板?

How can I stop a custom storyboard with a data trigger?

我有一个 canvas 的故事板,我想在 ShowError 属性 更改后立即停止它。

 <UserControl.Resources>
        <Storyboard x:Key="LoaderAnimation" Name="LoaderAnimation" >
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)" 
                                           Storyboard.TargetName="canvas" 
                                           RepeatBehavior="Forever">
                <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
                <EasingDoubleKeyFrame KeyTime="0:0:3" Value="360"/>
            </DoubleAnimationUsingKeyFrames>
        </Storyboard>
    </UserControl.Resources>


        <Viewbox x:Name="LayoutRoot" HorizontalAlignment="Center" VerticalAlignment="Top" Grid.Row="0" Height="150" Width="250">
            <Canvas Height="323" Width="308" RenderTransformOrigin="0.5,0.5" x:Name="canvas">
                <Canvas.RenderTransform>
                    <RotateTransform/>
                </Canvas.RenderTransform>

                <Ellipse Fill="{Binding Foreground, ElementName=userControl}" Height="71" Canvas.Left="121" Canvas.Top="21" Width="69" Opacity="0.3"/>
                <Ellipse Fill="{Binding Foreground, ElementName=userControl}" Height="71" Width="69" Canvas.Left="194" Canvas.Top="52" Opacity="0.4"/>
                <Ellipse Fill="{Binding Foreground, ElementName=userControl}" Width="69" Height="71" 

            </Canvas>
        </Viewbox>

        <TextBlock Grid.Row="1" TextWrapping="Wrap" FontSize="20" Margin="0 20 0 0">
            <TextBlock.Style>
                <Style TargetType="TextBlock">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding ShowError}" Value="false">
                            <Setter Property="Text" Value="{loc:Translate LogIn.TestSpsConnection}" />
                            <Setter Property="Foreground" Value="Black" />
                        </DataTrigger>
                        <DataTrigger Binding="{Binding ShowError}" Value="true">
                            <Setter Property="Text" Value="{loc:Translate LogIn.FailSpsConnection}" />
                            <Setter Property="Foreground" Value="Red" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </TextBlock.Style>
        </TextBlock>
        
    </Grid>

我已经尝试了所有想法,但我无法从 TextBlock 的样式访问故事板。 当 ShowError 变为真时,有什么办法可以停止动画吗? 谢谢你的帮助

尝试在您的 Canvas 上应用样式触发器以在 ShowError 变为真时停止情节提要,而不是从您的 TextBlock 中执行。

示例:

   <UserControl.Resources>
        <Storyboard x:Key="LoaderAnimation" Name="LoaderAnimation" >
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)" 
                                           RepeatBehavior="Forever">
                <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
                <EasingDoubleKeyFrame KeyTime="0:0:3" Value="360"/>
            </DoubleAnimationUsingKeyFrames>
        </Storyboard>
    </UserControl.Resources>

...

<Canvas Height="323" Width="308" RenderTransformOrigin="0.5,0.5" x:Name="canvas">
    ...
    <Canvas.Style>
        <Style TargetType="Canvas">
            <Style.Triggers>
                <EventTrigger RoutedEvent="Loaded">
                    <EventTrigger.Actions>
                        <BeginStoryboard x:Name="beginStoryboard" Storyboard="{StaticResource LoaderAnimation}"/>
                    </EventTrigger.Actions>
                </EventTrigger>
                <DataTrigger Binding="{Binding ShowError}" Value="True">
                    <DataTrigger.EnterActions>
                        <StopStoryboard BeginStoryboardName="beginStoryboard"/>
                    </DataTrigger.EnterActions>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Canvas.Style>
</Canvas>

此外,请注意我已从您的资源中删除了 Storyboard.TargetName,否则按我的方式使用会违反某些 XAML 规则。如果你把它放在那里,你会得到运行时错误。