2015-12-21 186 views
0

每當數據綁定填充顏色發生變化時,我都希望有一個橢圓填充「閃爍」(立即變白,然後返回新顏色)。這是我走到這一步:當填充/背景顏色發生變化時,WPF閃爍

<ctrls:NotifyEllipse Fill="{Binding Cluster.Brush, Converter={StaticResource CloneConverter}}" Width="10" Height="10" > 
    <ctrls:NotifyEllipse.Style> 
    <Style TargetType="{x:Type ctrls:NotifyEllipse}"> 
     <Style.Triggers> 
     <EventTrigger RoutedEvent="ctrls:NotifyEllipse.FillChanged"> 
      <BeginStoryboard> 
      <Storyboard AutoReverse="True"> 
       <ColorAnimation Storyboard.TargetProperty="(ctrls:NotifyEllipse.Fill).Color" To="White" Duration="0:0:1" /> 
      </Storyboard> 
      </BeginStoryboard> 
     </EventTrigger> 
     </Style.Triggers> 
    </Style> 
    </ctrls:NotifyEllipse.Style> 
</ctrls:NotifyEllipse> 

其中ctrls:NotifyEllipseUserControl只含有橢圓形,與依賴屬性Fill和路由事件FillChanged - 類似this answer。顏色改變的檢測部分效果很好,但是閃光部分(顯然)不能如我所願地工作:它首先將填充顏色更改爲新顏色,而不是緩慢變白,並在最後變回新顏色。 如上所述,目標是「閃光」 - 立即變白,然後返回到新的顏色。
請注意,有數據綁定在填充所以有兩個動畫一個即時和其他慢是不可能的,只是因爲我不知道該回哪個顏色:

<Storyboard> 
    <ColorAnimation Storyboard.TargetProperty="(ctrls:NotifyEllipse.Fill).Color" To="White" Duration="0:0:0" /> 
    <ColorAnimation Storyboard.TargetProperty="(ctrls:NotifyEllipse.Fill).Color" To="???" Duration="0:0:1" /> 
</Storyboard> 

積分:閃光白不立即但快速,並保持原來的顏色,直到全白到達,然後從白色慢慢轉爲新顏色,這將是理想的解決方案,但恐怕需要大量的自定義動畫代碼。如上所述的閃光就足夠了。

回答

2

剛剛從White啓動ColorAnimation:

<Storyboard> 
    <ColorAnimation Storyboard.TargetProperty="Fill.Color" 
        From="White" Duration="0:0:1" /> 
</Storyboard> 

爲了完整起見,我NotifyEllipse類看起來是這樣的:

public class NotifyEllipse : Shape 
{ 
    static NotifyEllipse() 
    { 
     FillProperty.AddOwner(typeof(NotifyEllipse), new FrameworkPropertyMetadata(
      (o, e) => 
      { 
       if (e.NewValue != e.OldValue) 
       { 
        ((NotifyEllipse)o).RaiseEvent(new RoutedEventArgs(FillChangedEvent)); 
       } 
      })); 
    } 

    public static readonly RoutedEvent FillChangedEvent = 
     EventManager.RegisterRoutedEvent(
      "FillChanged", RoutingStrategy.Bubble, 
      typeof(RoutedEventHandler), typeof(NotifyEllipse)); 

    public event RoutedEventHandler FillChanged 
    { 
     add { AddHandler(FillChangedEvent, value); } 
     remove { RemoveHandler(FillChangedEvent, value); } 
    } 

    protected override Geometry DefiningGeometry 
    { 
     get { return new EllipseGeometry(new Rect(RenderSize)); } 
    } 
} 
+0

正是這種簡單?我真的花了一天的時間解決這個問題......非常感謝 – wondra