禁用/启用XAML动画

时间:2015-04-21 09:40:35

标签: c# wpf xaml animation

我的XAML中有下一个触发器:

<ControlTemplate.Triggers>
    <DataTrigger Binding="{Binding Path=IsOpened}" Value="true">
        <DataTrigger.EnterActions>
            <BeginStoryboard>
                <Storyboard>
                    <DoubleAnimationUsingKeyFrames 
                        Storyboard.TargetName="Control"
                        Storyboard.TargetProperty="RenderTransform.(TranslateTransform.X)">
                        <LinearDoubleKeyFrame KeyTime="0:0:0.30" Value="-310"/>
                    </DoubleAnimationUsingKeyFrames>
                    <ObjectAnimationUsingKeyFrames 
                        Storyboard.TargetName="OpenHelpText" Storyboard.TargetProperty="Visibility">
                        <DiscreteObjectKeyFrame KeyTime="0:0:0.30" 
                                                Value="{x:Static Visibility.Hidden}"/>
                    </ObjectAnimationUsingKeyFrames>
                    <ObjectAnimationUsingKeyFrames 
                        Storyboard.TargetName="CloseHelpText" Storyboard.TargetProperty="Visibility">
                        <DiscreteObjectKeyFrame KeyTime="0:0:0.30" 
                                                Value="{x:Static Visibility.Visible}"/>
                    </ObjectAnimationUsingKeyFrames>
                    <ObjectAnimationUsingKeyFrames 
                        Storyboard.TargetName="ClosePanel" Storyboard.TargetProperty="Visibility">
                        <DiscreteObjectKeyFrame KeyTime="0:0:0.30" 
                                                Value="{x:Static Visibility.Visible}"/>
                    </ObjectAnimationUsingKeyFrames>
                    <ObjectAnimationUsingKeyFrames 
                        Storyboard.TargetName="OpenPanel" Storyboard.TargetProperty="Visibility">
                        <DiscreteObjectKeyFrame KeyTime="0:0:0.30" 
                                                Value="{x:Static Visibility.Hidden}"/>
                    </ObjectAnimationUsingKeyFrames>
                </Storyboard>
            </BeginStoryboard>
        </DataTrigger.EnterActions>
        <DataTrigger.ExitActions>
            <BeginStoryboard>
                <Storyboard>
                    <DoubleAnimationUsingKeyFrames 
                        Storyboard.TargetName="Control"
                        Storyboard.TargetProperty="RenderTransform.(TranslateTransform.X)">
                        <LinearDoubleKeyFrame KeyTime="0:0:0.30" Value="0"/>
                    </DoubleAnimationUsingKeyFrames>
                    <ObjectAnimationUsingKeyFrames 
                        Storyboard.TargetName="CloseHelpText" Storyboard.TargetProperty="Visibility">
                        <DiscreteObjectKeyFrame KeyTime="0:0:0.30" 
                                                Value="{x:Static Visibility.Hidden}"/>
                    </ObjectAnimationUsingKeyFrames>
                    <ObjectAnimationUsingKeyFrames 
                        Storyboard.TargetName="OpenHelpText" Storyboard.TargetProperty="Visibility">
                        <DiscreteObjectKeyFrame KeyTime="0:0:0.30" 
                                                Value="{x:Static Visibility.Visible}"/>
                    </ObjectAnimationUsingKeyFrames>
                    <ObjectAnimationUsingKeyFrames 
                        Storyboard.TargetName="ClosePanel" Storyboard.TargetProperty="Visibility">
                        <DiscreteObjectKeyFrame KeyTime="0:0:0.30" 
                                                Value="{x:Static Visibility.Hidden}"/>
                    </ObjectAnimationUsingKeyFrames>
                    <ObjectAnimationUsingKeyFrames 
                        Storyboard.TargetName="OpenPanel" Storyboard.TargetProperty="Visibility">
                        <DiscreteObjectKeyFrame KeyTime="0:0:0.30" 
                                                Value="{x:Static Visibility.Visible}"/>
                    </ObjectAnimationUsingKeyFrames>
                </Storyboard>
            </BeginStoryboard>
        </DataTrigger.ExitActions>
    </DataTrigger>
</ControlTemplate.Triggers>

我有一些动态参数可以告诉你动画是否启用。 (动态 - 表示应在参数值更改时启用/禁用动画)

禁用/启用动画的最佳方法是什么?

UPD :作为替代方案 - 可以在代码中创建相同的动画吗?

2 个答案:

答案 0 :(得分:2)

Refer this link like dis....

将元素名称绑定到主属性。这里我使用了Toggleswitch元素名称我用按钮绑定

 Visibility="{Binding IsOn,ElementName=togglebutton}, Converter={StaticResource BooleanToVisibilityConverter}}" />

答案 1 :(得分:1)

更新DataTrigger上的触发条件。

您已将动画嵌入DataTriggers中。

在您的情况下,也许您可​​以利用MultiDataTriggers来解决必须满足的多个条件。