I have code:
<UserControl.Resources>
<SolidColorBrush x:Key="KeysBorderBrush" Color="DimGray" />
</UserControl.Resources>
<ComboBox Name="Keys" HorizontalAlignment="Left" Margin="116,2,0,2" Width="122" BorderBrush="{DynamicResource KeysBorderBrush}" />
And codebehind:
SolidColorBrush keysBorderBrush;
ColorAnimation _keysAnimation;
public Constructor()
{
_keysAnimation = new ColorAnimation();
_keysAnimation.Completed += _keysAnimation_Completed;
}
private void Active_Checked(object sender, RoutedEventArgs e)
{
keysBorderBrush = (SolidColorBrush)this.FindResource("KeysBorderBrush");
keysBorderBrush.Color = Colors.Black;
_keysAnimation.To = Colors.Red;
_keysAnimation.AutoReverse = true;
_keysAnimation.RepeatBehavior = new RepeatBehavior(2);
_keysAnimation.DecelerationRatio = .1;
_keysAnimation.Duration = TimeSpan.FromMilliseconds(500);
keysBorderBrush.BeginAnimation(SolidColorBrush.ColorProperty, _keysAnimation);
}
void _keysAnimation_Completed(object sender, EventArgs e)
{
keysBorderBrush.Color = Colors.DimGray;
}
My goal:
Set the color to black, then animate, then return color to gray in _keysAnimation_Completed
event.
The problem in the _keysAnimation_Completed
event handler, it doesn't whant set the color back to DimGray
, it works just one time, in second+ times color stay in Black all of time, how can I fix it?
If you add _keysAnimation.FillBehavior = FillBehavior.Stop
, it will work as you expect it to.
Your Active_Checked
method should look like this:
private void Active_Checked(object sender, RoutedEventArgs e)
{
_keysBorderBrush = (SolidColorBrush) FindResource("KeysBorderBrush");
_keysBorderBrush.Color = Colors.Black;
_keysAnimation.To = Colors.Red;
_keysAnimation.AutoReverse = true;
_keysAnimation.RepeatBehavior = new RepeatBehavior(2);
_keysAnimation.DecelerationRatio = .1;
_keysAnimation.Duration = TimeSpan.FromMilliseconds(500);
_keysAnimation.FillBehavior = FillBehavior.Stop;
_keysBorderBrush.BeginAnimation(SolidColorBrush.ColorProperty, _keysAnimation);
}