2012-03-29 23 views
0

當有人將鼠標懸停在我的應用程序上時,我想將相同的動畫放到我的應用程序中的所有圖像。因此,我創建了以下樣式:從風格不起作用的動畫圖像

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

當我將鼠標懸停我的鼠標在該圖像獲取:

<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> 

而且我打算動畫我將應用樣式的圖像除外:

System.InvalidOperationException了未處理的消息=無法 動畫 '(0)' 上的不可變的對象實例。
源= PresentationFramework堆棧跟蹤: 在System.Windows.Media.Animation.Storyboard.VerifyPathIsAnimatable(的PropertyPath 路徑) 在System.Windows.Media.Animation.Storyboard.ClockTreeWalkRecursive(時鐘 currentClock,DependencyObject的containingObject,INameScope名稱範圍, 的DependencyObject parentObject,字符串parentObjectName,的PropertyPath parentPropertyPath,的HandoffBehavior的HandoffBehavior,HybridDictionary的 clockMappings,Int64的層) 在System.Windows.Media.Animation.Storyboard.ClockTreeWalkRecursive(時鐘 currentClock,DependencyObject的containingObject,INameScope名稱範圍, 的DependencyObject parentObjec T,字符串parentObjectName,的PropertyPath parentPropertyPath,的HandoffBehavior的HandoffBehavior,HybridDictionary的

等。

我有什麼改變的風格,使其工作?

回答

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>