2009-06-28 167 views
1

我有一個用戶控件,當我雙擊它時,我想讓它放大,如果它還沒有。如果是,那麼雙擊將縮小它。我可以讓它與代碼一起工作,但我無法在xaml中使用它。 下面是該句柄的雙擊事件背後的代碼。觸發動畫

void MyObjectMouseDoubleClick(object sender, MouseButtonEventArgs e) 
    { 
     if (IsZoomedIn) 
     { 
      IsZoomedIn = false; 
      //ZoomOutAnimation(); 
     } 
     else 
     { 
      IsZoomedIn = true; 
      //ZoomInAnimation(); 
     } 
    } 

然後在我的XAML:

<UserControl.RenderTransform> 
    <TransformGroup> 
     <RotateTransform /> 
     <ScaleTransform /> 
     <TranslateTransform /> 
    </TransformGroup> 
</UserControl.RenderTransform> 

<UserControl.Style> 
    <Style> 
     <Style.Triggers> 
      <Trigger Property="local:MyObject.IsZoomedIn" Value="False"> 
       <Trigger.EnterActions> 
        <BeginStoryboard> 
         <Storyboard> 
          <DoubleAnimation Storyboard.TargetProperty="(UserControl. 
          RenderTransform).(TransformGroup.Children)[1]. 
          (ScaleTransform.ScaleX)" To="1" Duration="0:0:.3" /> 
          <DoubleAnimation Storyboard.TargetProperty="(UserControl. 
          RenderTransform).(TransformGroup.Children)[1]. 
          (ScaleTransform.ScaleY)" To="1" Duration="0:0:.3" /> 
         </Storyboard> 
        </BeginStoryboard> 
       </Trigger.EnterActions> 
      </Trigger> 
      <Trigger Property="local:MyObject.IsZoomedIn" Value="True"> 
       <Trigger.EnterActions>   
       <BeginStoryboard> 
        <Storyboard> 
         <DoubleAnimation Storyboard.TargetProperty="(UserControl. 
         RenderTransform).(TransformGroup.Children)[1]. 
         (ScaleTransform.ScaleX)" To="2" Duration="0:0:.3" /> 
         <DoubleAnimation Storyboard.TargetProperty="(UserControl. 
         RenderTransform).(TransformGroup.Children)[1]. 
         (ScaleTransform.ScaleY)" To="2" Duration="0:0:.3" /> 
        </Storyboard> 
       </BeginStoryboard> 
       </Trigger.EnterActions> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 
</UserControl.Style> 

一旦放大了我的用戶,變焦出動畫不起作用。任何幫助讚賞。
謝謝。

回答

4

您的動畫持有這些值,因此即使觸發器正在觸發,第二個動畫也不會出現。您可以像使用EnterActions一樣使用Trigger.ExitActions,而不是使用兩個單獨的觸發器。

<Trigger Property="IsZoomedIn" 
     Value="True"> 
    <Trigger.EnterActions> 
     <BeginStoryboard> 
      <Storyboard > 
       <DoubleAnimation Storyboard.TargetProperty="(UserControl.RenderTransform).(TransformGroup.Children)[1].(ScaleTransform.ScaleX)"  
           To="2" 
           Duration="0:0:.3" /> 
       <DoubleAnimation Storyboard.TargetProperty="(UserControl.RenderTransform).(TransformGroup.Children)[1].(ScaleTransform.ScaleY)" 
           To="2" 
           Duration="0:0:.3" /> 
      </Storyboard> 
     </BeginStoryboard> 
    </Trigger.EnterActions> 
    <Trigger.ExitActions> 
     <BeginStoryboard> 
      <Storyboard> 
       <DoubleAnimation Storyboard.TargetProperty="(UserControl.RenderTransform).(TransformGroup.Children)[1].(ScaleTransform.ScaleX)" 
           To="1" 
           Duration="0:0:.3" /> 
       <DoubleAnimation Storyboard.TargetProperty="(UserControl.RenderTransform).(TransformGroup.Children)[1].(ScaleTransform.ScaleY)" 
           To="1" 
           Duration="0:0:.3" /> 
      </Storyboard> 
     </BeginStoryboard> 
    </Trigger.ExitActions> 
</Trigger> 
+0

謝謝!但是我仍然不明白爲什麼你的代碼有效。我知道用這種方式來思考它可能不是「正確的」,但是它是因爲EnterActions更像是「如果爲真」,並且ExitActions更像是「其他」?
謝謝。 – jkidv 2009-06-29 13:47:31