方法: ストーリーボードを使用してアニメーション化した後にプロパティを設定する

プロパティをアニメーション化した後に、その値を変更できないように見える場合があります。

SolidColorBrush の色をアニメーション化する

次の例では、StoryboardSolidColorBrush の色をアニメーション化するために使用されます。 ボタンをクリックすると、ストーリーボードがトリガーされます。 Completed イベントは、ColorAnimation が完了したときにプログラムに通知されるように処理されます。

<Button
  Content="Animate and Then Set Example 1">
  <Button.Background>
    <SolidColorBrush x:Name="Button1BackgroundBrush"
      Color="Red" />
  </Button.Background>
  <Button.Triggers>
    <EventTrigger RoutedEvent="Button.Click">
      <BeginStoryboard>
        <Storyboard>
          <ColorAnimation
            Storyboard.TargetName="Button1BackgroundBrush"
            Storyboard.TargetProperty="Color"
            From="Red" To="Yellow" Duration="0:0:5"
            FillBehavior="HoldEnd"
            Completed="setButton1BackgroundBrushColor" />
        </Storyboard>
      </BeginStoryboard>
    </EventTrigger>
  </Button.Triggers>
</Button>

ブラシの色を変更する

ColorAnimation が完了すると、プログラムはブラシの色を青色に変更しようとします。

private void setButton1BackgroundBrushColor(object sender, EventArgs e)
{

    // Does not appear to have any effect:
    // the brush remains yellow.
    Button1BackgroundBrush.Color = Colors.Blue;
}
Private Sub setButton1BackgroundBrushColor(ByVal sender As Object, ByVal e As EventArgs)

    ' Does not appear to have any effect:
    ' the brush remains yellow.
    Button1BackgroundBrush.Color = Colors.Blue
End Sub

前のコードは、何も実行しないように見えます。つまり、ブラシは、ブラシをアニメーション化した ColorAnimation によって指定された値である黄色のままです。 実際には、基になるプロパティ値 (基準値) は青色に変更されます。 しかし、有効な値または現在の値は黄色のままです。これは、ColorAnimation がまだ基準値をオーバーライドしているためです。 基準値が再度有効な値になるようにするには、アニメーションがプロパティに影響しないようにする必要があります。 ストーリーボード アニメーションでこれを行うには、次の 3 つの方法があります。

  • アニメーションの FillBehavior プロパティを Stop に設定します。

  • ストーリーボード全体を削除します。

  • アニメーションを個々のプロパティから削除します。

アニメーションの FillBehavior プロパティを Stop に設定する

FillBehaviorStop に設定して、有効期間の終了に達した後はターゲット プロパティへの影響を停止するようにアニメーションに指示します。

<Button
  Content="Animate and Then Set Example 2">
  <Button.Background>
    <SolidColorBrush x:Name="Button2BackgroundBrush"
      Color="Red" />
  </Button.Background>
  <Button.Triggers>
    <EventTrigger RoutedEvent="Button.Click">
      <BeginStoryboard>
        <Storyboard>
          <ColorAnimation
            Storyboard.TargetName="Button2BackgroundBrush"
            Storyboard.TargetProperty="Color"
            From="Red" To="Yellow" Duration="0:0:5"
            FillBehavior="Stop"
            Completed="setButton2BackgroundBrushColor" />
        </Storyboard>
      </BeginStoryboard>
    </EventTrigger>
  </Button.Triggers>
</Button>
private void setButton2BackgroundBrushColor(object sender, EventArgs e)
{

    // This appears to work:
    // the brush changes to blue.
    Button2BackgroundBrush.Color = Colors.Blue;
}
Private Sub setButton2BackgroundBrushColor(ByVal sender As Object, ByVal e As EventArgs)

    ' This appears to work:
    ' the brush changes to blue.
    Button2BackgroundBrush.Color = Colors.Blue
End Sub

ストーリーボード全体を削除する

RemoveStoryboard トリガーまたは Storyboard.Remove メソッドを使用して、ターゲット プロパティへの影響を停止するようにストーリーボード アニメーションに指示します。 この方法と FillBehavior プロパティの設定との違いは、ストーリーボードをいつでも削除できることにあります。一方、FillBehavior プロパティは、アニメーションが有効期間の終了に達したときにのみ効果があります。

<Button
  Name="Button3"
  Content="Animate and Then Set Example 3">
  <Button.Background>
    <SolidColorBrush x:Name="Button3BackgroundBrush"
      Color="Red" />
  </Button.Background>
  <Button.Triggers>
    <EventTrigger RoutedEvent="Button.Click">
      <BeginStoryboard Name="MyBeginStoryboard">
        <Storyboard x:Name="MyStoryboard">
          <ColorAnimation
            Storyboard.TargetName="Button3BackgroundBrush"
            Storyboard.TargetProperty="Color"
            From="Red" To="Yellow" Duration="0:0:5"
            FillBehavior="HoldEnd"
            Completed="setButton3BackgroundBrushColor" />
        </Storyboard>
      </BeginStoryboard>
    </EventTrigger>
  </Button.Triggers>
</Button>
private void setButton3BackgroundBrushColor(object sender, EventArgs e)
{

     // This appears to work:
    // the brush changes to blue.
    MyStoryboard.Remove(Button3);
    Button3BackgroundBrush.Color = Colors.Blue;
}
Private Sub setButton3BackgroundBrushColor(ByVal sender As Object, ByVal e As EventArgs)

     ' This appears to work:
    ' the brush changes to blue.
    MyStoryboard.Remove(Button3)
    Button3BackgroundBrush.Color = Colors.Blue
End Sub

アニメーションを個々のプロパティから削除する

アニメーションがプロパティに影響しないようにするもう 1 つの手法は、アニメーション化されるオブジェクトの BeginAnimation(DependencyProperty, AnimationTimeline) メソッドを使用することです。 アニメーション化するプロパティを最初のパラメーターとして指定し、null を 2 番目のパラメーターとして指定します。

<Button
  Name="Button4"
  Content="Animate and Then Set Example 4">
  <Button.Background>
    <SolidColorBrush x:Name="Button4BackgroundBrush"
      Color="Red" />
  </Button.Background>
  <Button.Triggers>
    <EventTrigger RoutedEvent="Button.Click">
      <BeginStoryboard>
        <Storyboard>
          <ColorAnimation
            Storyboard.TargetName="Button4BackgroundBrush"
            Storyboard.TargetProperty="Color"
            From="Red" To="Yellow" Duration="0:0:5"
            FillBehavior="HoldEnd"
            Completed="setButton4BackgroundBrushColor" />
        </Storyboard>
      </BeginStoryboard>
    </EventTrigger>
  </Button.Triggers>
</Button>
private void setButton4BackgroundBrushColor(object sender, EventArgs e)
{

     // This appears to work:
    // the brush changes to blue.
    Button4BackgroundBrush.BeginAnimation(SolidColorBrush.ColorProperty, null);
    Button4BackgroundBrush.Color = Colors.Blue;
}
Private Sub setButton4BackgroundBrushColor(ByVal sender As Object, ByVal e As EventArgs)

     ' This appears to work:
    ' the brush changes to blue.
    Button4BackgroundBrush.BeginAnimation(SolidColorBrush.ColorProperty, Nothing)
    Button4BackgroundBrush.Color = Colors.Blue
End Sub

この手法は、非ストーリーボード アニメーションにも使用できます。

関連項目