WPF ColorAnimation for a Brush property WPF ColorAnimation for a Brush property wpf wpf

WPF ColorAnimation for a Brush property


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();

(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.


            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 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!