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 


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.