2012-11-17 45 views
0

我是WPF的新手,有一個可能愚蠢的問題。如何用相同的動畫逐個動畫不同的按鈕,WPF

我試圖用相同的動畫(旋轉360度)動畫4個按鈕時,他們中的一個被點擊,只有這一個得到動畫。

這是我到目前爲止有:

<Window.Resources> 
    <Storyboard x:Key="Storyboard" BeginTime="00:00:00" Duration="00:00:10"> 
     <DoubleAnimationUsingKeyFrames Storyboard.TargetName="rotButton" Storyboard.TargetProperty="(RotateTransform.Angle)"> 
      <SplineDoubleKeyFrame KeyTime="0:0:00.0" Value="0.0" /> 
      <SplineDoubleKeyFrame KeyTime="0:0:01.0" Value="360.0" /> 
     </DoubleAnimationUsingKeyFrames> 
    </Storyboard> 

</Window.Resources> 

而且rotButton在第一個按鈕定義在這裏:

<Button Click="Button_Click"> 
      <StackPanel> 
       <Image Source="open.png" Height="46" Width="48" /> 
      </StackPanel> 
      <Button.RenderTransform> 
       <TransformGroup> 
        <RotateTransform x:Name="rotButton" Angle="0" CenterX="25" CenterY="25" /> 
        <ScaleTransform x:Name="scaButton" ScaleX="1" ScaleY="1" CenterX="50" CenterY="25" /> 
       </TransformGroup> 
      </Button.RenderTransform> 
      <Button.Triggers> 
       <EventTrigger RoutedEvent="Button.Click"> 
        <BeginStoryboard Storyboard="{StaticResource Storyboard}" /> 
       </EventTrigger> 
      </Button.Triggers> 
     </Button> 

我如何可以使用此代碼對所有其他按鈕,並有「共同「Button.RenderTransform每個按鈕?應該有更多更智能的方式來創建3個更多的故事板,併爲每個按鈕使用rotButton1,rotButton2等。

我希望這是有道理的,並指出我在正確的方向:)

感謝

回答

1

如果你創建你的按鈕樣式,你可以使用一個setter設置的RenderTransform對按鈕的每個實例使用該樣式。另外,樣式可以具有觸發器。

的訣竅是正確的路徑語法http://blogs.charteris.com/blogs/patl-closed/archive/2007/03/20/Complex-PropertyPath-syntax.aspx

<Window.Resources> 
    <TransformGroup x:Key="transformGroup"> 
     <RotateTransform Angle="0" CenterX="25" CenterY="25" /> 
     <ScaleTransform ScaleX="1" ScaleY="1" CenterX="50" CenterY="25" /> 
    </TransformGroup> 
    <Style x:Key="MyButtonStyle" TargetType="{x:Type Button}"> 
     <Setter Property="RenderTransform" Value="{StaticResource transformGroup}"/> 
     <Style.Triggers> 
      <EventTrigger RoutedEvent="Button.Click"> 
       <BeginStoryboard> 
        <Storyboard BeginTime="00:00:00" Duration="00:00:10"> 
         <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(Button.RenderTransform).(TransformGroup.Children)[0].(RotateTransform.Angle)"> 
          <SplineDoubleKeyFrame KeyTime="0:0:00.0" Value="0.0" /> 
          <SplineDoubleKeyFrame KeyTime="0:0:01.0" Value="360.0" /> 
         </DoubleAnimationUsingKeyFrames> 
        </Storyboard> 
       </BeginStoryboard> 
      </EventTrigger> 
     </Style.Triggers> 
    </Style> 
</Window.Resources> 
<Grid> 
    <StackPanel> 
     <Button Style="{StaticResource MyButtonStyle}"/> 
     <Button Style="{StaticResource MyButtonStyle}"/>   
    </StackPanel> 
</Grid> 
+0

是的,我知道,但不知道如何做到這一點正是..不能找到很好的例子。你有一些嗎? – Pepys

+0

動畫讓我大笑 – user1834059

+0

就在現場;)非常感謝兄弟......是的,這是愚蠢的動畫,但這是我的第一個想法。謝謝 – Pepys