动画无法正常使用数据触发器

时间:2011-08-18 09:25:34

标签: wpf xaml animation

我有一个组合框,它与动画的枚举绑定。我们的要求是,如果在组合框中选择了动画样式,则应在文本块上触发相应的动画。我有3个动画,即淡入淡出,缩放和眨眼。虽然眨眼工作正常,但褪色和缩放​​不起作用。它仅在屏幕加载时第一次起作用,然后,即使选择更改为淡入/缩放,也不会触发动画。好像现在,我已经使用数据触发器触发了动画,这就是当这个问题开始出现时,但是当我们尝试使用事件触发器时动画被完美地触发了。我的代码如下。有人可以帮助我解决我做错了什么以及如何纠正它。

动画:

        <Storyboard x:Key="Fade">
        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)">
            <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}"/>
        </ObjectAnimationUsingKeyFrames>
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)">
            <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
            <EasingDoubleKeyFrame KeyTime="0:0:2" Value="1"/>
        </DoubleAnimationUsingKeyFrames>
    </Storyboard>
    <Storyboard x:Key="Scale">
        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)">
            <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}"/>
        </ObjectAnimationUsingKeyFrames>
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)">
            <EasingDoubleKeyFrame KeyTime="0" Value="1"/>
            <EasingDoubleKeyFrame KeyTime="0:0:0.7" Value="1"/>
        </DoubleAnimationUsingKeyFrames>
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)">
            <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
            <EasingDoubleKeyFrame KeyTime="0:0:0.7" Value="1"/>
        </DoubleAnimationUsingKeyFrames>
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)">
            <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
            <EasingDoubleKeyFrame KeyTime="0:0:0.7" Value="1"/>
        </DoubleAnimationUsingKeyFrames>
    </Storyboard>
<Storyboard x:Key="Blink">
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)">
                <EasingDoubleKeyFrame KeyTime="0" Value="1"/>
            </DoubleAnimationUsingKeyFrames>
            <ObjectAnimationUsingKeyFrames RepeatBehavior="3x" Storyboard.TargetProperty="(UIElement.Visibility)">
                <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}"/>
                <DiscreteObjectKeyFrame KeyTime="0:0:0.25" Value="{x:Static Visibility.Collapsed}"/>
                <DiscreteObjectKeyFrame KeyTime="0:0:0.5" Value="{x:Static Visibility.Visible}"/>
            </ObjectAnimationUsingKeyFrames>
        </Storyboard>

用于触发动画的组合框,文本块和数据触发器:

         <telerik:RadComboBox x:Name="animationTypeComboBox"
                                 Foreground="White"  
                                 Width="180" 
                                 ItemsSource="{Binding Source={StaticResource AnimationTypeEnumValues}}" 
                                 SelectedItem="{Binding SelectedAnimationType, Mode=TwoWay, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}" 
                                 VerticalAlignment="Center" >
            </telerik:RadComboBox>
            <TextBlock x:Name="testMsg" Text="Test message" VerticalAlignment="Center" HorizontalAlignment="Center" RenderTransformOrigin="0.5,0.5">
                <TextBlock.RenderTransform>
                        <TransformGroup>
                            <ScaleTransform/>
                            <SkewTransform/>
                            <RotateTransform/>
                            <TranslateTransform/>
                        </TransformGroup>
                    </TextBlock.RenderTransform>
                 <TextBlock.Style>
                    <Style TargetType="TextBlock" BasedOn="{StaticResource Subheading}">    
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding SelectedAnimationType}" Value="{x:Static ent:AnimationType.Fade}">
                                <DataTrigger.EnterActions>
                                    <BeginStoryboard Storyboard="{StaticResource Fade}"/>
                                </DataTrigger.EnterActions>
                            </DataTrigger>
                            <DataTrigger Binding="{Binding SelectedAnimationType}" Value="{x:Static ent:AnimationType.Scale}">
                                <DataTrigger.EnterActions>
                                    <BeginStoryboard Storyboard="{StaticResource Scale}"/>
                                </DataTrigger.EnterActions>
                            </DataTrigger>
                            <DataTrigger Binding="{Binding SelectedAnimationType}" Value="{x:Static ent:AnimationType.Blink}">
                                <DataTrigger.EnterActions>
                                    <BeginStoryboard Storyboard="{StaticResource Blink}"/>
                                </DataTrigger.EnterActions>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </TextBlock.Style>
            </TextBlock>

1 个答案:

答案 0 :(得分:3)

我对您的代码进行了一些更改,以使其在我的本地系统中运行....这是您问题的解决方案..

<TextBlock x:Name="testMsg" Text="Test message" VerticalAlignment="Center" HorizontalAlignment="Center" RenderTransformOrigin="0.5,0.5">
                <TextBlock.RenderTransform>
                        <TransformGroup>
                            <ScaleTransform/>
                            <SkewTransform/>
                            <RotateTransform/>
                            <TranslateTransform/>
                        </TransformGroup>
                    </TextBlock.RenderTransform>
                 <TextBlock.Style>
                    <Style TargetType="TextBlock">    
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding SelectedItem,ElementName=AnimStyles}" Value="Fade">
                                <DataTrigger.EnterActions>
                                      <RemoveStoryboard BeginStoryboardName="Scale"/>
                                    <RemoveStoryboard BeginStoryboardName="Blink"/>
                                    <BeginStoryboard  Name="Fade">
                                         <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)">
                                                <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
                                                <EasingDoubleKeyFrame KeyTime="0:0:2" Value="1"/>
                                            </DoubleAnimationUsingKeyFrames>
                                             </Storyboard>
                                    </BeginStoryboard>
                                </DataTrigger.EnterActions>
                            </DataTrigger>
                            <DataTrigger Binding="{Binding SelectedItem,ElementName=AnimStyles}" Value="Scale">
                                <DataTrigger.EnterActions>
                                     <RemoveStoryboard BeginStoryboardName="Fade"/>
                                    <RemoveStoryboard BeginStoryboardName="Blink"/>
                                    <BeginStoryboard Name="Scale">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)">
                                                <EasingDoubleKeyFrame KeyTime="0" Value="1"/>
                                                <EasingDoubleKeyFrame KeyTime="0:0:0.7" Value="1"/>
                                            </DoubleAnimationUsingKeyFrames>
                                            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)">
                                                <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
                                                <EasingDoubleKeyFrame KeyTime="0:0:0.7" Value="1"/>
                                            </DoubleAnimationUsingKeyFrames>
                                            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)">
                                                <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
                                                <EasingDoubleKeyFrame KeyTime="0:0:0.7" Value="1"/>
                                            </DoubleAnimationUsingKeyFrames>
                                         </Storyboard>
                                    </BeginStoryboard>
                                </DataTrigger.EnterActions>
                            </DataTrigger>
                            <DataTrigger Binding="{Binding SelectedItem,ElementName=AnimStyles}" Value="Blink">
                                <DataTrigger.EnterActions>
                                    <RemoveStoryboard BeginStoryboardName="Fade"/>
                                    <RemoveStoryboard BeginStoryboardName="Scale"/>
                                    <BeginStoryboard Name="Blink">
                                         <Storyboard>
                                            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)">
                                                <EasingDoubleKeyFrame KeyTime="0" Value="1"/>
                                            </DoubleAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames RepeatBehavior="3x" Storyboard.TargetProperty="(UIElement.Visibility)">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}"/>
                                                <DiscreteObjectKeyFrame KeyTime="0:0:0.25" Value="{x:Static Visibility.Collapsed}"/>
                                                <DiscreteObjectKeyFrame KeyTime="0:0:0.5" Value="{x:Static Visibility.Visible}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </BeginStoryboard>
                                </DataTrigger.EnterActions>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </TextBlock.Style>
        </TextBlock>

在上面的代码中,而不是在资源中使用故事板,然后排队...并在启动实际故事板之前删除其他故事板... 这意味着以前的故事板所影响的行为将被删除,每次动画都像新的一样工作..