C# 如何停止动画WPF?

C# 如何停止动画WPF?,c#,.net,wpf,xaml,animation,C#,.net,Wpf,Xaml,Animation,如何停止动画,使其不会产生Completed事件。下面是一个简单的例子 <Window x:Class="WpfApplication5.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" He

如何停止动画,使其不会产生
Completed
事件。下面是一个简单的例子

<Window x:Class="WpfApplication5.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="248" Width="318">
    <Grid>
        <Border Width="20" Height="20" Background="Red" MouseEnter="Border_MouseEnter" MouseLeave="Border_MouseLeave" x:Name="border" />
    </Grid>
</Window>
若在矩形变为白色之前将鼠标移出,一段时间后仍将显示弹出窗口。如何预防?让我们假设
Border\u MouseLeave
Border\u MouseEnter
方法彼此不了解(它们不能相互传递动画实例变量)。

您可以使用以下方法:

<Border Width="20" Height="20" Background="Red" x:Name="border" >
                <Border.Triggers>
                    <EventTrigger RoutedEvent="MouseEnter">
                        <BeginStoryboard Name="Ali">
                            <Storyboard>
                                <DoubleAnimation To="0" Duration="0:0:4" Completed="com" Storyboard.TargetProperty="Opacity"/>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                    <EventTrigger RoutedEvent="MouseLeave">
                        <StopStoryboard  BeginStoryboardName="Ali"/>
                    </EventTrigger>
                </Border.Triggers>
            </Border>

您可以使用属性或数据触发器的Enteraction和ExitActions属性,或者如@Ali所说,正确使用开始和停止情节提要

<Border Width="20" Height="20" Background="Red" x:Name="border" >
                <Border.Triggers>
                    <EventTrigger RoutedEvent="MouseEnter">
                        <BeginStoryboard Name="Ali">
                            <Storyboard>
                                <DoubleAnimation To="0" Duration="0:0:4" Completed="com" Storyboard.TargetProperty="Opacity"/>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                    <EventTrigger RoutedEvent="MouseLeave">
                        <StopStoryboard  BeginStoryboardName="Ali"/>
                    </EventTrigger>
                </Border.Triggers>
            </Border>
private void com(object sender, EventArgs e)
        {
            MessageBox.Show("boom!");
        }