如何删除样式中定义的WPF动画?

时间:2010-11-10 23:49:19

标签: wpf animation

我在删除WPF中的属性动画时遇到问题。当使用DataTrigger启动故事板时,我无法像在其他情况下那样从属性中删除动画。无论我尝试什么或在哪里:OrientationProperty都锁定到动画的结束值。您可以在此示例中看到此信息,因为在故事板完成后无法旋转ScatterViewItem。

这是XAML:

<s:SurfaceWindow x:Class="SurfaceApplication1.SurfaceWindow1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:s="http://schemas.microsoft.com/surface/2008"
    x:Name="_this"
    Title="SurfaceApplication1"
    >
    <s:SurfaceWindow.Resources>
        <ImageBrush x:Key="WindowBackground" Stretch="None" Opacity="0.6" ImageSource="pack://application:,,,/Resources/WindowBackground.jpg"/>

        <Storyboard x:Key="flipForward" Completed="FlipCompleted">
            <DoubleAnimation By="180" 
                         FillBehavior="HoldEnd"
                         Duration="0:0:0.5"
                         Storyboard.TargetProperty="(s:ScatterViewItem.Orientation)" />
        </Storyboard>

        <Storyboard x:Key="flipBackward" Completed="FlipCompleted">
            <DoubleAnimation By="-180"
                         FillBehavior="HoldEnd"
                         Duration="0:0:0.5"
                         Storyboard.TargetProperty="(s:ScatterViewItem.Orientation)" />
        </Storyboard>
    </s:SurfaceWindow.Resources>


    <Grid Background="{StaticResource WindowBackground}" >
        <s:ScatterView>
            <s:ScatterViewItem x:Name="_item" Orientation="0">
                <s:ScatterViewItem.Style>
                    <Style TargetType="{x:Type s:ScatterViewItem}" BasedOn="{StaticResource {x:Type s:ScatterViewItem}}">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding ElementName=_button,Path=IsChecked}" Value="True">
                                <DataTrigger.EnterActions>
                                    <BeginStoryboard>
                                        <StaticResource ResourceKey="flipForward" />
                                    </BeginStoryboard>
                                </DataTrigger.EnterActions>
                                <DataTrigger.ExitActions>
                                    <BeginStoryboard>
                                        <StaticResource ResourceKey="flipBackward" />
                                    </BeginStoryboard>
                                </DataTrigger.ExitActions>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </s:ScatterViewItem.Style>

                <StackPanel>
                    <s:SurfaceToggleButton Margin="20" x:Name="_button">Click Me!</s:SurfaceToggleButton>
                </StackPanel>

            </s:ScatterViewItem>
        </s:ScatterView>
    </Grid>
</s:SurfaceWindow>

这是背后的相关代码:

    private void FlipCompleted(object sender, EventArgs e)
    {
        _item.BeginAnimation(ScatterViewItem.OrientationProperty, null); // Doesn't work
        ((sender as ClockGroup).Timeline as Storyboard).Remove(_item); // Doesn't work either
        ((sender as ClockGroup).Timeline as Storyboard).Remove(); // Neither does this
    }

有没有人提示如何从OrientationProperty中删除动画?

1 个答案:

答案 0 :(得分:1)

就像您使用BeginStoryboard添加故事板一样,您也可以在XAML中使用RemoveStoryboard删除。

 <DataTrigger.EnterActions>
     <RemoveStoryboard BeginStoryboardName="flipBackward" />
 </DataTrigger.EnterActions>

然而我很惊讶BeginAnimation(ScatterViewItem.OrientationProperty, null);没有做到这一点。恕我直言,它应该... ...