Stuart Blackler
Stuart Blackler

Reputation: 3772

Animate TextBlock colour change

Is there a way to animate a TextBlock's colour change?

At the moment I am basically using the enter/leave events to change the colour and I would like to a almost like a fade (but a fast fade, so .1/.2 secs) to give it a nicer visual appearance instead of being instantaneous.

Any advice on the best/easiest way to do this?

ps. Due to constraints, the actual code is vb.net but I will accept c#.net answers as I can read both fine. Just learning WPF.

ta

Upvotes: 0

Views: 423

Answers (1)

ChrisF
ChrisF

Reputation: 137148

You want a ColorAnimation. There's an example on that page either in XAML:

<!-- Animates the brush's color to orange
     when the mouse leaves the rectangle. -->
<EventTrigger RoutedEvent="Rectangle.MouseLeave">
  <BeginStoryboard>
    <Storyboard>
      <ColorAnimation
        Storyboard.TargetName="MyAnimatedBrush"
        Storyboard.TargetProperty="Color"
        To="Orange" Duration="0:0:1" />
    </Storyboard>
  </BeginStoryboard>
</EventTrigger> 

or in code:

'
' Animate the brush's color to orange when
' the mouse leaves the rectangle.
'
Dim mouseLeaveColorAnimation As New ColorAnimation()
mouseLeaveColorAnimation.To = Colors.Orange
mouseLeaveColorAnimation.Duration = TimeSpan.FromSeconds(1)
Storyboard.SetTargetName(mouseLeaveColorAnimation, "MyAnimatedBrush")
Storyboard.SetTargetProperty(mouseLeaveColorAnimation, New PropertyPath(SolidColorBrush.ColorProperty))
Dim mouseLeaveStoryboard As New Storyboard()
mouseLeaveStoryboard.Children.Add(mouseLeaveColorAnimation)
AddHandler aRectangle.MouseLeave, Sub(sender As Object, e As MouseEventArgs) mouseLeaveStoryboard.Begin(Me)

Upvotes: 1

Related Questions