当有人将鼠标悬停在应用程序中的所有图像上时,我希望将相同的动画放置到它们上。因此,我创建了以下样式:
<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未经处理消息=无法在不可变对象实例上动画化"(0)"
Source=PresentationFrame StackTrace:位于System.Windows.Media.Animation.Storyboard.VerifyPathIsAnimatable(PropertyPath路径)位于System.Windows.Media.Manimation.Storyboard.ClockTreeWalkRecursive(时钟currentClock,DependencyObject containingObject,INameScope nameScope,DependencyObject parentObject,字符串parentObjectName,PropertyPathparentPropertyPath,切换行为切换行为,HybridDictionaryclockMappings,Int64层)位于System.Windows.Media.Manimation.Storyboard.ClockTreeWalkRecursive(时钟currentClock,DependencyObject containingObject,INameScope nameScope,DependencyObject parentObject,字符串parentObjectName,PropertyPathparentPropertyPath,切换行为切换行为,HybridDictionary等等。。
我必须改变什么样的风格才能让它发挥作用?
只需移除故事板目标。那就行了。
<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>