2013-10-15 185 views
1

我在停止動畫時遇到了一些問題,它會一直持續下去。我會認爲RepeatBehavior應該控制這一點,但它似乎不起作用。WPF使用RepeatBehavior停止ColorAnimation

<Grid.Style> 
    <Style TargetType="Grid"> 
     <Style.Triggers> 
      <DataTrigger Binding="{Binding IsNew}" Value="true"> 
       <DataTrigger.EnterActions> 
        <BeginStoryboard> 
         <Storyboard> 
          <ColorAnimation RepeatBehavior="1" 
           Storyboard.TargetProperty="Background.(SolidColorBrush.Color)" 
           To="LightGreen" Duration="0:0:0.25" AutoReverse="True" > 
          </ColorAnimation> 
         </Storyboard> 
        </BeginStoryboard> 
       </DataTrigger.EnterActions> 
      </DataTrigger> 
      <DataTrigger Binding="{Binding IsNew}" Value="false"> 
       <Setter Property="Background" Value="WhiteSmoke"></Setter> 
      </DataTrigger> 
     </Style.Triggers> 
    </Style> 
</Grid.Style> 

回答

3

的解決方案是使用

RepeatBehavior="1x" 

所以1X不僅僅是,不是很符合邏輯的我,但有可能是一個原因..

更新(輸入後來自@clemens):

根據MSDN的規定,XAML屬性的用法是:

<object property="iterationCountx"/> 
- or - 
<object property="[days.]hours:minutes:seconds[.fractionalSeconds]"/> 
- or - 
<object property="[days.]hours:minutes"/> 
- or - 
<object property="days"/> 
- or - 
<object property="Forever"/> 

這使得很多更有意義,但不是很直觀..

<Grid.Style> 
    <Style TargetType="Grid"> 
     <Style.Triggers> 
      <DataTrigger Binding="{Binding IsNew}" Value="true"> 
       <DataTrigger.EnterActions> 
        <BeginStoryboard> 
         <Storyboard> 
          <ColorAnimation RepeatBehavior="1x" 
           Storyboard.TargetProperty="Background.(SolidColorBrush.Color)" 
           To="LightGreen" Duration="0:0:0.25" AutoReverse="True" > 
          </ColorAnimation> 
         </Storyboard> 
        </BeginStoryboard> 
       </DataTrigger.EnterActions> 
      </DataTrigger> 
      <DataTrigger Binding="{Binding IsNew}" Value="false"> 
       <Setter Property="Background" Value="WhiteSmoke"></Setter> 
      </DataTrigger> 
     </Style.Triggers> 
    </Style> 
</Grid.Style> 
+7

原因很簡單:'RepeatBehavior = 「1」'指定*一個天*,而'RepeatBehavior =「1倍「'指定*一次*。您可能需要查看[MSDN上的RepeatBehavior文檔](http://msdn.microsoft.com/library/system.windows.media.animation.timeline.repeatbehavior.aspx)。 – Clemens

+0

@克萊門斯感謝您的意見。我更新了我的答案幷包含了MSDN代碼。 – Tejo