Brush 属性的 WPF ColorAnimation
我想知道是否有人可以帮助我 - 我有一个标签,当在后面的代码中调用方法时,我需要能够在任意两种颜色之间交叉淡入淡出。
到目前为止我最好的尝试:
Private OldColor as Color = Colors.White
Sub SetPulseColor(ByVal NewColor As Color)
Dim F As New Animation.ColorAnimation(OldColor, NewColor, New Duration(TimeSpan.Parse("00:00:01")))
OldColor = NewColor
F.AutoReverse = False
PulseLogo.BeginAnimation(Label.ForegroundProperty, F)
End Sub
我遇到的问题是 ColorAnimation 返回 Media.Color 并且 Foreground 的属性类型是 Brush。
我知道如何创建适当的画笔,但不知道如何在动画中执行此操作。
从谷歌搜索来看,我似乎需要一个转换器:
<ValueConversion(GetType(SolidColorBrush), GetType(SolidColorBrush))> _
Public Class ColorConverter
Implements IValueConverter
Public Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
Dim Color As Color = DirectCast(value, Color)
Return New SolidColorBrush(Color)
End Function
Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
Return Nothing
End Function
End Class
但是我见过的所有示例都将其绑定到 XAML 中的动画 - 而且我想在后面的代码中执行此操作...
有人可以指出我正确的方向吗?
谢谢
I wonder if someone can help me - I've got a label which I need to be able to cross-fade between any 2 colors when a method is called in the code behind.
My best attempt so far:
Private OldColor as Color = Colors.White
Sub SetPulseColor(ByVal NewColor As Color)
Dim F As New Animation.ColorAnimation(OldColor, NewColor, New Duration(TimeSpan.Parse("00:00:01")))
OldColor = NewColor
F.AutoReverse = False
PulseLogo.BeginAnimation(Label.ForegroundProperty, F)
End Sub
The problem I have is that ColorAnimation returns a Media.Color and The property type for Foreground is Brush.
I know how to create the appropriate brush but not how to do it in an animation.
From Googling, it seems I need a converter:
<ValueConversion(GetType(SolidColorBrush), GetType(SolidColorBrush))> _
Public Class ColorConverter
Implements IValueConverter
Public Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
Dim Color As Color = DirectCast(value, Color)
Return New SolidColorBrush(Color)
End Function
Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
Return Nothing
End Function
End Class
but all the examples I've seen bind it to the animation in XAML - And I'd like to do it in the code behind...
Can someone please point me in the right direction?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通常的解决方案是不使用转换器,而是对画笔的颜色进行动画处理。但是,要执行此操作,您需要一个 PropertyPath,这又意味着您需要一个情节提要:(
请原谅 C# 语法)
请注意 SetTargetProperty 调用中的属性路径,该路径向下遍历 Foreground 属性并进入生成的画笔的 Color 属性。
您还可以使用此技术为渐变画笔等中的各个渐变停止点设置动画。
The usual solution to this is not to use a converter, but instead to animate the Color of the Brush. However, to do this you need a PropertyPath, which in turn means you need a storyboard:
(pardon C# syntax)
Note the property path in the SetTargetProperty call, which traverses down through the Foreground property and into the resulting brush's Color property.
You can also use this technique to animate individual gradient stops in a gradient brush, etc.
//变量颜色& BaseColour是Color类,timeSpan是TimeSpan类,BackGroundCellGrid是Grid类;
//无需在 XAML 中创建 SolidColorBrush 并绑定到它;
//玩得开心!
//VariableColour & BaseColour are class of Color, timeSpan is Class of TimeSpan, BackGroundCellGrid is class of Grid;
//no need to create SolidColorBrush and binding to it in XAML;
//have fun!