如何:为已到达活动时段结束的时间线指定填充行为(FillBehavior)

此示例演示如何指定 FillBehavior 动画属性的非活动 Timeline 状态。

示例:

TimelineFillBehavior 属性确定一个动画属性在没有进行动画时的处理方式,也就是说,当 Timeline 处于非活动状态时,它的父级 Timeline 仍处于活动或保持期内。 例如,动画属性在动画结束时是否保留在其结束值,或者它是否还原回动画开始前的值?

以下示例使用一个 DoubleAnimation 对两个矩形进行 Width 动画处理。 每个矩形使用不同的 Timeline 对象。

Timeline 有一个 FillBehavior 被设置为 Stop,这会导致矩形的宽度在 Timeline 结束时恢复到其非动画值。 另一个Timeline有一个FillBehaviorHoldEnd,导致宽度在Timeline结束时保持不变,停留在其最终值。

<Page 
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
  <StackPanel Margin="20">
    <Border Background="#99FFFFFF">
      <TextBlock Margin="20">
              This example shows how the FillBehavior property determines how an animation behaves
              after it reaches the end of its duration.
      </TextBlock>
    </Border>
      
    <TextBlock>FillBehavior="Deactivate"</TextBlock>
    <Rectangle Name="deactiveAnimationRectangle" Width="20" Height="20" Fill="#AA3333FF" HorizontalAlignment="Left" >
      <Rectangle.Triggers>
        <EventTrigger RoutedEvent="Rectangle.Loaded">
          <BeginStoryboard>
            <Storyboard>

              <!-- The animated rectangle's width reverts back to its non-animated value
                   after the animation ends. -->
              <DoubleAnimation 
                Storyboard.TargetName="deactiveAnimationRectangle" 
                Storyboard.TargetProperty="Width" 
                From="100" To="400" Duration="0:0:2" FillBehavior="Stop" />
            </Storyboard>
          </BeginStoryboard>
        </EventTrigger>
      </Rectangle.Triggers>
    </Rectangle>

    <TextBlock Margin="0,20,0,0">FillBehavior="HoldEnd" </TextBlock>
    <Rectangle Name="holdEndAnimationRectangle" Width="20" Height="20" Fill="#AA3333FF" HorizontalAlignment="Left" >
      <Rectangle.Triggers>
        <EventTrigger RoutedEvent="Rectangle.Loaded">
          <BeginStoryboard>
            <Storyboard>

              <!-- The animated rectangle's width remains at its end value after the 
                   animation ends. -->
              <DoubleAnimation Storyboard.TargetName="holdEndAnimationRectangle" 
                Storyboard.TargetProperty="Width"  
                From="100" To="400" Duration="0:0:2" FillBehavior="HoldEnd" />
            </Storyboard>
          </BeginStoryboard>
        </EventTrigger>
      </Rectangle.Triggers>
    </Rectangle>
  </StackPanel>
</Page>

有关完整示例,请参阅 动画示例库

另请参阅