I need help in taking right decision. I need to animate a background color of my user control when some event happens. When it is, I want to change the background just for 1 second and then turn it back. Which way should I go? Use color animation or timer or may by some other way.
Solved. Thanks to all! This works good for me:
ColorAnimation animation;
animation = new ColorAnimation();
animation.From = Colors.Orange;
animation.To = Colors.Gray;
animation.Duration = new Duration(TimeSpan.FromSeconds(1));
this.elGrid.Background.BeginAnimation(SolidColorBrush.ColorProperty, animation);
I would use an
EventTrigger
with aColorAnimation
.In this example a
Button Brackground
goes green on aMouseLeave
event. This code is hopefully similar to what you may need.//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!
Watch out, you could received a System.InvalidOperationException if your background is a frozen instance.
To correct this message assign the background of your control to a non-frozen instance.
Freezable Objects Overview on MSDN
You can use
DoubleAnimation
to change the color like this:Hope it helps
In WPF ,Using animation maybe better. Expression blend have the relative animation/behaviour.
This worked well for me.
I have a path inside a button (it draws an "X"):
On mouse over, I want the cross to go red, so I add:
The most confusing thing about this is the brackets within
Storyboard.TargetProperty
. If you remove the brackets, nothing works.For more information, see "propertyName Grammar" and "Storyboard.TargetProperty".