动画TextBlock颜色更改

时间:2011-09-13 20:18:29

标签: wpf vb.net animation

有没有办法让TextBlock颜色变化动画?

目前我基本上使用进入/离开事件来改变颜色,我想要一个几乎像褪色(但快速褪色,所以.1 / .2秒),以使其更好的视觉外观而不是瞬间。

有关最佳/最简单方法的建议吗?

PS。由于约束,实际代码是vb.net但我会接受c#.net答案,因为我可以读得很好。刚刚学习WPF。

TA

1 个答案:

答案 0 :(得分:2)

你想要一个ColorAnimation。在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> 

或代码:

'
' 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)