ColorAnimation,完成的事件不能再工作一次

ColorAnimation, Completed event doesn't work more then once

我有代码:

<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 事件中将 return 颜色设置为灰色。

_keysAnimation_Completed 事件处理程序中的问题,它没有将颜色设置回 DimGray,它只工作一次,在第二次以上颜色一直保持黑色,我该如何解决?

如果您添加 _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);
}