I managed to find out how to make a WPF animation - transition between two colors.
It's called ColorAnimation and works well.
ColorAnimation animation = new ColorAnimation
{
From = Colors.DarkGreen,
To = Colors.Transparent,
Duration = new Duration(TimeSpan.FromSeconds(1.5)),
AutoReverse = false
};
animation.Completed += new EventHandler(animation_Completed);
SolidColorBrush brush = new SolidColorBrush(Colors.Transparent);
animation.AccelerationRatio = 0.5;
Background = brush;
brush.BeginAnimation(SolidColorBrush.ColorProperty, animation);
I am using this to animate background of my usercontrol. My controls background is SolidColorBrush
. Recently I changed to LinearGradientBrush
. Now I can use my animation no more.
I need animation from brush to brush, not color to color. And best option is Abstract brush type, which includes SolidColor, LinearGradient etc, so I can animate for example from SolidColorBrush
to LinearGradientBrush
. Is that even possible? Thank you.
You just need to use the color animation on the gradient stops of the gradient brush. Here is an example that animates a rectangle gradient using a storyboard.
You can animate the color of the brush if you have a template style in which you give the fill brush a name, like so:
As taken from MSDN
Another possible way is, to create a custom animtion class that animate brushes. I found a simple way to do that by creating a class, derivated from
AnimationTimeline
. We can override some members in the custom class, among other things theAnimationTimeline.GetCurrentValue
method. It returns a value depend on the animation progress and the start- and end value.The simplest way is to create a
VisualBrush
and crossfade the start- with the end value with theOpacity
property on a child control. The result is a class like the following:You can use it as always in XAML:
or in code behind:
It is also possible to extend the
BrushAnimation
with constructor overloads etc., so it looks like a .NET given animation type.