触发器和动画

时间:2013-08-06 11:02:54

标签: wpf xaml animation triggers

我必须遵循XAML

<Window.Style>
    <Style TargetType="{x:Type Window}">
        <Setter Property="Opacity" Value="0.8" />
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Opacity" Value="1" />
            </Trigger>
            <EventTrigger RoutedEvent="FrameworkElement.Loaded">
                <BeginStoryboard>
                    <BeginStoryboard.Storyboard>
                        <Storyboard>
                            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity">
                                <EasingDoubleKeyFrame KeyTime="0" Value="0" />
                                <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="0.8" />
                            </DoubleAnimationUsingKeyFrames>
                        </Storyboard>
                    </BeginStoryboard.Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </Style.Triggers>


    </Style>
</Window.Style>

当我有EventTrigger时,MouseOver触发器无效。没有EventTrigger工作正常。我怎么能同时拥有这两个?

1 个答案:

答案 0 :(得分:4)

动画将不透明度设置为最终值,你需要给它一个停止的FillBehavior。

<Window.Style>
        <Style TargetType="{x:Type Window}">
            <Setter Property="Opacity" Value="0.8" />
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Opacity" Value="1" />
                </Trigger>
                <EventTrigger RoutedEvent="FrameworkElement.Loaded">
                    <BeginStoryboard>
                        <BeginStoryboard.Storyboard>
                            <Storyboard>
                                <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity" FillBehavior="Stop">
                                    <EasingDoubleKeyFrame KeyTime="0" Value="0" />
                                    <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="0.8" />
                                </DoubleAnimationUsingKeyFrames>
                            </Storyboard>
                        </BeginStoryboard.Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Style.Triggers>


        </Style>
    </Window.Style>