重新触发WPF动画

时间:2011-07-13 15:44:07

标签: wpf xaml animation datatrigger

我有以下WPF XAML,它试图根据复选框的IsChecked属性设置2个矩形的可见性。因此Checked表示蓝色框出现,红色框在一秒后消失,Unchecked表示红色框出现,蓝色框消失,Undetermined表示没有可见。 我正在使用故事板,但我遗漏了一些东西。它适用于第一个触发器,但有些动画属性卡住了。

  <DataTemplate x:Key="dt">
                <DockPanel>
                    <CheckBox x:Name="ckToggle" Content="Toggle" DockPanel.Dock="Top" IsThreeState="True"/>
                    <StackPanel Orientation="Horizontal">
                        <Rectangle x:Name="a" Visibility="Hidden" Width="100" Height="100" Fill = "Red"/>
                        <Rectangle x:Name="b" Visibility="Hidden" Width="100" Height="100" Fill = "Blue"/>
                    </StackPanel>
                </DockPanel>

                <DataTemplate.Triggers>

                    <DataTrigger Binding="{Binding ElementName=ckToggle, Path=IsChecked}" Value="True">
                         <DataTrigger.EnterActions>
                            <BeginStoryboard>
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="a" Storyboard.TargetProperty="Visibility">
                                        <DiscreteObjectKeyFrame KeyTime="00:00:01" Value="{x:Static Visibility.Visible}"/>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </BeginStoryboard>
                            <BeginStoryboard>
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="b" Storyboard.TargetProperty="Visibility">
                                        <DiscreteObjectKeyFrame KeyTime="00:00:00" Value="{x:Static Visibility.Visible}"/>
                                        <DiscreteObjectKeyFrame KeyTime="00:00:01" Value="{x:Static Visibility.Hidden}"/>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </BeginStoryboard>
                        </DataTrigger.EnterActions> 
                         <DataTrigger.ExitActions>
                        </DataTrigger.ExitActions> 

                    </DataTrigger>

                    <DataTrigger Binding="{Binding ElementName=ckToggle, Path=IsChecked}" Value="False">
                           <DataTrigger.EnterActions>
                            <BeginStoryboard>
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="b" Storyboard.TargetProperty="Visibility"  >
                                        <DiscreteObjectKeyFrame KeyTime="00:00:01" Value="{x:Static Visibility.Visible}"/>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </BeginStoryboard>
                            <BeginStoryboard>
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="a" Storyboard.TargetProperty="Visibility">
                                         <DiscreteObjectKeyFrame KeyTime="00:00:00" Value="{x:Static Visibility.Visible}"/>
                                            <DiscreteObjectKeyFrame KeyTime="00:00:01" Value="{x:Static Visibility.Hidden}" />
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </BeginStoryboard>
                        </DataTrigger.EnterActions> 
                         <DataTrigger.ExitActions>
                        </DataTrigger.ExitActions> 
                    </DataTrigger>
                </DataTemplate.Triggers>
            </DataTemplate>

     <ContentControl ContentTemplate="{StaticResource dt}"/>

如果有人可以提供帮助,谢谢 布赖恩

1 个答案:

答案 0 :(得分:0)

您遇到的问题是wpf故事板的属性。这样的情况是,当你开始一个故事板时,它永远不会停止,直到你告诉它。因此,当您拨打第二个故事板时,您拨打的第一个故事板仍在运行。走出去的方法如下: 首先将每个触发器的两个故事板合并为一个(您可以将多个动画放在一个故事板中,您不必这样做,但它会使事情变得更容易......)。现在为剩下的两个BeginStoryboard调用中的每一个添加一个名称,并将具有相同名称的StopStoryboard调用添加到exitActions块中的每个dataTrigger。我最终得到了以下代码(希望)可以按你的意愿工作:

<Window x:Class="Test.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Test"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <DataTemplate x:Key="dt">
        <DockPanel>
            <CheckBox x:Name="ckToggle" Content="Toggle" DockPanel.Dock="Top" IsThreeState="True"/>
            <StackPanel Orientation="Horizontal">
                <Rectangle x:Name="a" Visibility="Hidden" Width="100" Height="100" Fill = "Red"/>
                <Rectangle x:Name="b" Visibility="Hidden" Width="100" Height="100" Fill = "Blue"/>
            </StackPanel>
        </DockPanel>
        <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding ElementName=ckToggle, Path=IsChecked}" Value="True">
                <DataTrigger.EnterActions>
                    <BeginStoryboard Name="st1">
                        <Storyboard>
                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="a" Storyboard.TargetProperty="Visibility">
                                <DiscreteObjectKeyFrame KeyTime="00:00:01" Value="{x:Static Visibility.Visible}"/>
                            </ObjectAnimationUsingKeyFrames>
                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="b" Storyboard.TargetProperty="Visibility">
                                <DiscreteObjectKeyFrame KeyTime="00:00:00" Value="{x:Static Visibility.Visible}"/>
                                <DiscreteObjectKeyFrame KeyTime="00:00:01" Value="{x:Static Visibility.Hidden}"/>
                            </ObjectAnimationUsingKeyFrames>
                        </Storyboard>
                    </BeginStoryboard>
                </DataTrigger.EnterActions>
                <DataTrigger.ExitActions>
                    <StopStoryboard BeginStoryboardName="st1"/>
                </DataTrigger.ExitActions>
            </DataTrigger>
            <DataTrigger Binding="{Binding ElementName=ckToggle, Path=IsChecked}" Value="False">
                <DataTrigger.EnterActions>
                    <BeginStoryboard Name="st2">
                        <Storyboard>
                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="b" Storyboard.TargetProperty="Visibility">
                                <DiscreteObjectKeyFrame KeyTime="00:00:01" Value="{x:Static Visibility.Visible}"/>
                            </ObjectAnimationUsingKeyFrames>
                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="a" Storyboard.TargetProperty="Visibility">
                                <DiscreteObjectKeyFrame KeyTime="00:00:00" Value="{x:Static Visibility.Visible}"/>
                                <DiscreteObjectKeyFrame KeyTime="00:00:01" Value="{x:Static Visibility.Hidden}" />
                            </ObjectAnimationUsingKeyFrames>
                        </Storyboard>
                    </BeginStoryboard>
                </DataTrigger.EnterActions>
                <DataTrigger.ExitActions>
                    <StopStoryboard BeginStoryboardName="st2"/>
                </DataTrigger.ExitActions>
            </DataTrigger>
        </DataTemplate.Triggers>
    </DataTemplate>
</Window.Resources>
<ContentControl ContentTemplate="{StaticResource dt}"/></Window>