2013-07-10 43 views
1

我有一個自定義button-style,其中ColorAnimation重複按下按鈕時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> 

我該如何解決這個問題?

回答

1

您還沒有在您的ColorAnimation上設置From屬性。因此,當您在動畫中間按下按鈕時,情節串聯板將當前的Foreground顏色值作爲其From,這是動畫反轉回來的顏色。

現在當您反覆按下按鈕時,From顏色會越來越接近紅色,給人的印象是顏色卡在紅色。


更新:

這個答案只點出了問題。請參閱薇薇的回答了一個優雅的解決

+0

這確實發生了什麼。首先它是一種微弱的「白紅」色,然後是粉紅色。最終它變紅。 – DeMama

3

更新

星爺,如果您不能負擔刪除StoryboardTrigger.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> 
+0

我試過了,但這讓我的'StoryBoard'幾乎看不見。它閃爍..我增加了「持續時間」,但它保持不變,「Vanlalhriata's'答案解決了它。 – DeMama

+0

@DeMama哦,這是因爲即使在動畫未完成時觸發器退出也會調用RemoveStoryboard。但是,指定一個硬編碼的「From」並不是唯一的解決方案。我已經用Alternate方法更新了我的答案,通過在Animation開始時不指定From或To Value來讓Animation自動重置。這樣做的唯一好處是不指定一個硬編碼的「From」並記錄所有將來更新的控件'Style' – Viv

+0

+1 Thanks man;) – DeMama