WPF ColorAnimation的画笔属性 [英] WPF ColorAnimation for a Brush property

查看:1558
本文介绍了WPF ColorAnimation的画笔属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道是否有人能帮助我 - 我有一个标签,我需要能够对任何2颜色之间的交叉淡入淡出当一个方法在code被称为落后。

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

我的问题是,ColorAnimation返回Media.Color和物业类型前景刷。

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

但我已经看到了将其绑定到动画在XAML中的所有例子 - 我想这样做在code背后...

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?

感谢

推荐答案

通常的解决方法是不使用转换器,而是以动画画笔的颜色。然而,要做到这一点,你需要的PropertyPath,这反过来又意味着你需要一个故事板:

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:

Storyboard s = new Storyboard();
s.Duration = new Duration(new TimeSpan(0, 0, 1));
s.Children.Add(F);

Storyboard.SetTarget(F, PulseLogo);
Storyboard.SetTargetProperty(F, new PropertyPath("Foreground.Color"));

s.Begin();

(原谅C#语​​法)

(pardon C# syntax)

请注意在SetTargetProperty呼叫,它通过前台财产,到生成画笔的颜色属性将遍历属性路径。

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.

这篇关于WPF ColorAnimation的画笔属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆