I want to make a continuous brush animation whenever the mouse is over my usercontrol.
I already have this:
private void MainControl_MouseEnter(object sender, MouseEventArgs e)
{
BeginColorAnimation(Background, BackgroundHover);
}
private void BeginColorAnimation(Brush from, Brush to)
{
BrushAnimation anim = new BrushAnimation()
{
From = from,
To = to,
Duration = AnimationDuration
};
BackgroundBorder.BeginAnimation(Border.BackgroundProperty, anim);
}
private void MainControl_MouseLeave(object sender, MouseEventArgs e)
{
BeginColorAnimation(BackgroundHover, Background);
}
The BrushAnimation class can be found here
The problem i have right now is that if i move the mouse quickly over the control, the Background of my control shifts from the Background brush to the BackgroundHover brush (or viceversa) instantly and i don't want that.
How can i make this to look like a continuous change between brushes?
EDIT 1
Here you can see a video of the problem i have.
Let me explain myself:
Imagine we have two brushes, one black and one blue (or any other color). When the mouse is over the control i want to change the background of my color from black to blue, and when the mouse exits the control i want to change the background back to black. So what happens if the mouse is over the control and exits halfway the animation? It looks like if was bouncing from one brush to another. I want to avoid that.
I tried using:
private void MainControl_MouseEnter(object sender, MouseEventArgs e)
{
BeginColorAnimation(BackgroundBorder.Background, BackgroundHover);
}
private void MainControl_MouseLeave(object sender, MouseEventArgs e)
{
BeginColorAnimation(BackgroundBorder.Background, Background);
}
But then i receive: 'System.InvalidOperationException' This freezable cannot be frozen.
EDIT 2
Ended up using a SolidColorBrush instead of a Brush, and using the ColorAnimation class.
public bool BackgroundFrozen = true;
private void MainControl_MouseEnter(object sender, MouseEventArgs e)
{
BeginColorAnimation((SolidColorBrush)BackgroundBorder.Background, BackgroundHover);
}
private void BeginColorAnimation(SolidColorBrush from, SolidColorBrush to)
{
if(BackgroundFrozen)
{
BackgroundBorder.Background = BackgroundBorder.Background.CloneCurrentValue();
BackgroundFrozen = false;
}
ColorAnimation anim = new ColorAnimation()
{
From = from.Color,
To = to.Color,
Duration = AnimationDuration
};
BackgroundBorder.Background.BeginAnimation(SolidColorBrush.ColorProperty, anim);
}
private void MainControl_MouseLeave(object sender, MouseEventArgs e)
{
BeginColorAnimation((SolidColorBrush)BackgroundBorder.Background, Background);
}
For some reason the Border Background property is frozen at first, so i used a BackgroundFrozen bool to "Unfreeze" it with CloneCurrentValue() when the animation runs for first time.
Result here