我有代码:
<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}" />
代码隐藏:
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;
}
我的目标:
将颜色设置为黑色,然后设置动画,然后在_keysAnimation_Completed
事件中将颜色返回灰色。
_keysAnimation_Completed
事件处理程序中的问题,它没有将颜色设置回DimGray
,它只能运行一次,在第二次+次颜色保持在黑色的所有时间,我该如何解决?
答案 0 :(得分:1)
如果您添加_keysAnimation.FillBehavior = FillBehavior.Stop
,它将按预期工作。
您的Active_Checked
方法应如下所示:
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);
}