从样式无效的动画图像

时间:2012-03-29 15:22:31

标签: c# wpf xaml animation styles

当有人将鼠标悬停在我的应用程序上时,我想将相同的动画放置到我的所有图像中。结果我创建了以下样式:

<Style x:Key="test" TargetType="{x:Type Image}">

        <Style.Resources>
            <Storyboard x:Key="Storyboard1">
                <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Width)" 
                    Storyboard.Target="{Binding RelativeSource={RelativeSource Self}}">
                    <EasingDoubleKeyFrame KeyTime="0:0:1" Value="200">
                        <EasingDoubleKeyFrame.EasingFunction>
                            <BackEase EasingMode="EaseOut"/>
                        </EasingDoubleKeyFrame.EasingFunction>
                    </EasingDoubleKeyFrame>
                </DoubleAnimationUsingKeyFrames>
            </Storyboard>
        </Style.Resources>

        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Trigger.EnterActions>
                    <BeginStoryboard Storyboard="{StaticResource Storyboard1}"/>
                </Trigger.EnterActions>
            </Trigger>
        </Style.Triggers>
</Style>

在我计划制作动画的图像上,我会将该样式应用为:

  <Image Style="{StaticResource test}" Name="image1" Source="/PDV;component/images/t.png" Stretch="Uniform" Width="100" />

当我将鼠标悬停在该图像上时,我得到了例外:

  

System.InvalidOperationException未处理Message =无法   在不可变对象实例上动画'(0)'   Source = PresentationFramework StackTrace:          在System.Windows.Media.Animation.Storyboard.VerifyPathIsAnimatable(PropertyPath)   路径)          在System.Windows.Media.Animation.Storyboard.ClockTreeWalkRecursive(时钟   currentClock,包含Object的DependencyObject,INameScope nameScope,   DependencyObject parentObject,String parentObjectName,PropertyPath   parentPropertyPath,HandoffBehavior handoffBehavior,HybridDictionary   clockMappings,Int64层)          在System.Windows.Media.Animation.Storyboard.ClockTreeWalkRecursive(时钟   currentClock,包含Object的DependencyObject,INameScope nameScope,   DependencyObject parentObject,String parentObjectName,PropertyPath   parentPropertyPath,HandoffBehavior handoffBehavior,HybridDictionary

     

等。

我需要更改哪种风格才能使其正常工作?

1 个答案:

答案 0 :(得分:2)

只需删除故事板目标。它会正常工作。

<Style x:Key="test" TargetType="{x:Type Image}">

    <Style.Resources>
        <Storyboard x:Key="Storyboard1">
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Width)" 
                **Storyboard.Target="{Binding RelativeSource={RelativeSource Self}}"**>
                <EasingDoubleKeyFrame KeyTime="0:0:1" Value="200">
                    <EasingDoubleKeyFrame.EasingFunction>
                        <BackEase EasingMode="EaseOut"/>
                    </EasingDoubleKeyFrame.EasingFunction>
                </EasingDoubleKeyFrame>
            </DoubleAnimationUsingKeyFrames>
        </Storyboard>
    </Style.Resources>

    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Trigger.EnterActions>
                <BeginStoryboard Storyboard="{StaticResource Storyboard1}"/>
            </Trigger.EnterActions>
        </Trigger>
    </Style.Triggers>