WPF ColorAnimation in Code

In WPF you have the liberty to either create your animation in either XAML (xml format) or you can simply do it in the code behind file using C#. Here, I will also use a code-behind approach to animate the background Color of a button (say, buttonName).

SolidColorBrush brush = new SolidColorBrush(Colors.Red);
buttonName.Background = brush;

ColorAnimation animation = new ColorAnimation(Colors.Red, Colors.Blue, 
			   new Duration(TimeSpan.FromSeconds(5)));

animation.AutoReverse = true;
animation.RepeatBehavior = RepeatBehavior.Forever;
brush.BeginAnimation(SolidColorBrush.ColorProperty, animation);

NOTE:  you cannot initialize brush to Brushes.Red (for example) because that object is frozen and cannot be animated.

Refer it for more info 

Advertisements