2010-02-12 88 views
14

我不知道是否有人可以帮助我 - 我有一个标签,当需要在后面的代码中调用一个方法时,我需要能够在任何两种颜色之间进行交叉淡入淡出。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和物业类型前景刷。

我知道如何创建合适的画笔,但不知道如何在动画中做到这一点。

从谷歌搜索,看来我需要一个转换器:

<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的所有例子 - 我想做到这一点在后面的代码。 ..

有人能指点我在正确的方向吗?

感谢

回答

21

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

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#语法)

注意在SetTargetProperty调用属性路径,它通过前景将遍历属性以及生成的画笔的颜色属性。

也可以使用该技术进行动画各个梯度停止在渐变画笔等

+0

这真的很高雅 - 我现在就试试。 [编辑]错误:无法为'System.Windows.Media.SolidColorBrush'上的'Color'属性设置动画,因为对象被封闭或冻结。 [我的代码]:\t \t昏暗BR作为的SolidColorBrush = DirectCast((PulseLogo.Foreground)的SolidColorBrush) \t \t PulseLogo.Foreground.BeginAnimation(SolidColorBrush.ColorProperty,F) “感谢您的帮助 – Basic 2010-02-12 01:09:57

+0

对不起,伙计,我弄乱。我以前只用故事板做过,我天真地认为我可以直接将它翻译成BeginAnimation调用,这是错误的。我已经更新了答案,现在用实际的\ * gasp \ *诚实善良的测试代码 - 希望这对你更好。 – itowlson 2010-02-12 01:40:27

+0

C#不用担心 - 它们非常相似,只不过是一种方言:)感谢您提供更新的解决方案 - 它的工作原理完美无瑕。 – Basic 2010-02-13 20:35:31

0
  ColorAnimation colorChangeAnimation = new ColorAnimation(); 
      colorChangeAnimation.From = VariableColour; 
      colorChangeAnimation.To = BaseColour; 
      colorChangeAnimation.Duration = timeSpan; 

      PropertyPath colorTargetPath = new PropertyPath("(Panel.Background).(SolidColorBrush.Color)"); 
      Storyboard CellBackgroundChangeStory = new Storyboard(); 
      Storyboard.SetTarget(colorChangeAnimation, BackGroundCellGrid); 
      Storyboard.SetTargetProperty(colorChangeAnimation, colorTargetPath); 
      CellBackgroundChangeStory.Children.Add(colorChangeAnimation); 
      CellBackgroundChangeStory.Begin(); 

// VariableColour & BaseColour是类别Color的,时间跨度是时间跨度的类,BackGroundCellGrid是类网格的;

//无需创建SolidColorBrush并在XAML中对其进行绑定; //玩得开心!