在WPF中禁用Storyboard或Animation

时间:2015-03-10 19:15:21

标签: c# wpf animation storyboard

我创建了一个包含动画和一些消息的简单通知窗口。但是,我可以禁用例如动画或故事板。 MouseEnter事件,就像Facebook通知一样。逐渐降低不透明度,当我在窗口上拖动鼠标时,将不透明度设置为100%。怎么做? 这是一个xaml代码:

 WindowStyle="None" AllowsTransparency="True" Background="Transparent" >
<Grid x:Name="gridData" RenderTransformOrigin="0,1" MouseRightButtonDown="Window_MouseRightButtonDown" MouseEnter="Grid_MouseEnter">
    <Border BorderThickness="1" Background="SkyBlue" BorderBrush="Black" CornerRadius="10">
        <StackPanel Margin="20">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="32"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="40"/>
                </Grid.ColumnDefinitions>
                <TextBlock x:Name="txtTitle" Grid.Row="0" Grid.Column="0" Text="" FontWeight="Bold" VerticalAlignment="Center"/>
                <Image x:Name="image" Grid.Row="0" Grid.Column="1" HorizontalAlignment="Left" Visibility="Collapsed"/>
                <TextBlock x:Name="txtMessage" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Text="" TextWrapping="Wrap"/>
            </Grid>
        </StackPanel>
    </Border>

    <Grid.Triggers>
        <EventTrigger RoutedEvent="FrameworkElement.Loaded" >
            <BeginStoryboard >
                <Storyboard>
                    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(ScaleTransform.ScaleY)">
                        <SplineDoubleKeyFrame KeyTime="0:0:0" Value="0"/>
                        <SplineDoubleKeyFrame KeyTime="0:0:0.5" Value="1"/>
                    </DoubleAnimationUsingKeyFrames>
                    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)">
                        <SplineDoubleKeyFrame KeyTime="0:0:2" Value="1"/>
                        <SplineDoubleKeyFrame KeyTime="0:0:4" Value="0"/>
                    </DoubleAnimationUsingKeyFrames>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Grid.Triggers>

    <Grid.RenderTransform>
        <ScaleTransform ScaleY="1" />
    </Grid.RenderTransform>

</Grid>

1 个答案:

答案 0 :(得分:1)

BeginStoryboard添加姓名:

<BeginStoryboard Name="ScaleAndFadeOut">

然后为另一个事件添加另一个事件触发器,并使用StopStoryboard元素:

<Grid.Triggers>
    <EventTrigger RoutedEvent="FrameworkElement.Loaded">
        ...
    </EventTrigger>

    <EventTrigger RoutedEvent="FrameworkElement.MouseMove">
        <StopStoryboard BeginStoryboardName="ScaleAndFadeOut" />
    </EventTrigger>
</Grid.Triggers>

MSDN:"How to: Use Event Triggers to Control a Storyboard After It Starts"