简体   繁体   English

如果少于 1 秒,WPF 表单不透明度淡入淡出动画未完成

[英]WPF form opacity fade animation not finishing if less then 1 second

I have a form that I would like to have fade to 70% opacity unless the mouse if over the form the code I have only works properly if I set the animation time to 1 second.我有一个表单,我希望将其淡化到 70% 的不透明度,除非鼠标悬停在表单上方,否则我的代码只有在将动画时间设置为 1 秒时才能正常工作。 Anything less then that and the animation seems to stop before getting back to 100% opacity.低于此值,动画似乎在恢复到 100% 不透明度之前停止。

<Window.Resources>
    <Style TargetType="local:MainWindow">
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Trigger.EnterActions>
                    <BeginStoryboard>
                        <Storyboard
                                Storyboard.TargetProperty="(Window.Opacity)"
                                Duration="0:0:0.3">
                            <DoubleAnimation To="1" />
                        </Storyboard>
                    </BeginStoryboard>
                </Trigger.EnterActions>
                <Trigger.ExitActions>
                    <BeginStoryboard>
                        <Storyboard
                                Storyboard.TargetProperty="(Window.Opacity)"
                                Duration="0:0:0.3">
                            <DoubleAnimation To=".70" />
                        </Storyboard>
                    </BeginStoryboard>
                </Trigger.ExitActions>
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

Storyboard.TargetProperty and Duration must be set on the animations, not on the Storyboard. Storyboard.TargetPropertyDuration必须在动画上设置,而不是在 Storyboard 上。

If you set the Duration on the Storyboard, the animations run for their default Duration of 1 second.如果您在 Storyboard 上设置 Duration,动画会按照其默认的 1 秒持续时间运行。

<Trigger Property="IsMouseOver" Value="True">
    <Trigger.EnterActions>
        <BeginStoryboard>
            <Storyboard>
                <DoubleAnimation
                    Storyboard.TargetProperty="Opacity"
                    To="1" Duration="0:0:0.3"/>
            </Storyboard>
        </BeginStoryboard>
    </Trigger.EnterActions>
    <Trigger.ExitActions>
        <BeginStoryboard>
            <Storyboard>
                <DoubleAnimation
                    Storyboard.TargetProperty="Opacity"
                    To=".7" Duration="0:0:0.3" />
            </Storyboard>
        </BeginStoryboard>
    </Trigger.ExitActions>
</Trigger>

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

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