1

我有一个button-style习惯ColorAnimation

这很好用,但是当反复按下多次时,它会停留在目标颜色上。

<Style TargetType="Button" x:Key="mainButton">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}">
                    <ContentPresenter Content="{TemplateBinding ContentControl.Content}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsPressed" Value="True">
                        <Trigger.EnterActions>
                            <BeginStoryboard>
                                <Storyboard>
                                    <ColorAnimation 
                                    Duration="0:0:0.10" 
                                    Storyboard.TargetProperty="(Foreground).(SolidColorBrush.Color)" 
                                    To="Red"
                                    AutoReverse="True"/>
                                </Storyboard>
                            </BeginStoryboard>
                        </Trigger.EnterActions>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我该如何解决这个问题?

4

2 回答 2

3

更新

是的,如果您负担不起删除Storyboardin的费用,Trigger.ExitActions那么您确实必须自己解决From中间起始的问题Storyboard

但是,指定硬编码From 并不是唯一的解决方案。您可以让动画在启动时将其自身重置为基础基色。

这样做的好处是通过不指定一个From你有更少的东西来跟踪未来的更新。

<Storyboard AutoReverse="True">

  <!-- By not specifying a To or From we pretty much reset the property to un-animated state(Exactly what the hard-coded from does) -->
  <ColorAnimation Duration="0:0:0"
                  Storyboard.TargetProperty="(Foreground).(SolidColorBrush.Color)" />

  <!-- This part is same as original time to kick into new Foreground as desired -->
  <ColorAnimation Duration="0:0:1.5"
                  Storyboard.TargetProperty="(Foreground).(SolidColorBrush.Color)"
                  To="Red" />
</Storyboard>
于 2013-07-10T10:07:28.243 回答
1

您尚未FromColorAnimation. 因此,当您在其动画中间按下按钮时,Storyboard 将当前Foreground颜色值作为其From,这就是动画反转回的颜色。

现在,当您反复按下按钮时,From颜色会越来越接近红色,给人的印象是颜色卡在红色上。


更新:

这个答案只指出了问题。请参阅 Viv 的答案以获得优雅的解决方案

于 2013-07-10T10:05:58.867 回答