停止wpf Storyboard并保留实际值

本文关键字:保留 wpf Storyboard 停止 | 更新日期: 2023-09-27 18:18:05

我有以下wpf代码

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary>
                <Image x:Key="MyImage" Source="../Assets/icon.ico"/>
            </ResourceDictionary>
        </ResourceDictionary.MergedDictionaries>
        <Storyboard x:Key="AkomiLogoAnimation">
            <DoubleAnimation
                            Storyboard.TargetProperty="(Image.RenderTransform).(RotateTransform.Angle)"
                            By="360"
                            SpeedRatio="0.5"
                            RepeatBehavior="Forever"
                            FillBehavior="Stop"
                            />
        </Storyboard>
    </ResourceDictionary>
</Window.Resources>
        <TextBox x:Name="txtAuto"/>
                <Image Source="../Assets/icon.ico" Width="20" Height="20" RenderTransformOrigin=".5,.5">
                <Image.Resources>
                    <converter:IsNullConverter x:Key="IsNullConverter"/>
                </Image.Resources>
                <Image.RenderTransform>
                <RotateTransform Angle="0" />
            </Image.RenderTransform>
                <Image.Style>
                    <Style>
                    <Style.Triggers>
                            <DataTrigger Binding="{Binding ElementName=txtAuto, Path=Text, Converter={StaticResource IsNullConverter}, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}" Value="False">
                            <DataTrigger.EnterActions>
                                <BeginStoryboard Name="AkomiRotation" Storyboard="{StaticResource AkomiLogoAnimation}">
                                </BeginStoryboard>
                            </DataTrigger.EnterActions>
                        </DataTrigger>
                            <DataTrigger Binding="{Binding  ElementName=txtAuto, Path=Text, Converter={StaticResource IsNullConverter}, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}" Value="True">
                                <DataTrigger.EnterActions>
                                <StopStoryboard BeginStoryboardName="AkomiRotation" >
                                </StopStoryboard>
                            </DataTrigger.EnterActions>
                        </DataTrigger>
                    </Style.Triggers>
                    </Style>
                </Image.Style>
            </Image>

目标是在输入一些文本后立即旋转图像。但是停止storyboard会让图像角度跳转到它的初始值。

我试图将FillBehavior="HoldEnd"添加到故事板定义中,但这不起作用。

有什么建议吗?也许我可以在动画结束时访问角度值并在RotateTransform

停止wpf Storyboard并保留实际值

中设置这个值

您尝试过PauseStoryboard而不是stop吗?此外,您应该更改动画

的填充行为。