填充/背景颜色更改时WPF闪烁

时间:2015-12-21 12:08:12

标签: c# wpf colors

每当数据绑定填充颜色发生变化时,我希望有一个椭圆填充"闪烁"(立即变为白色,然后返回到新颜色)。这是我到目前为止所得到的:

<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:NotifyEllipse UserControl仅包含椭圆,依赖属性Fill和路由事件FillChanged - 类似this answer。颜色变化的检测部分效果很好,但是闪光部分(显然)不能像我想的那样工作:它首先将填充颜色改变为新颜色,而不是慢慢变白并且最后变回新颜色。 如上所述,目标是&#34; flash&#34; - 立即变白,然后返回新颜色。
请注意,Fill上有数据绑定,因此有一个瞬间有两个动画,而其他慢动作是不可能的,因为我不知道要返回哪个颜色:

<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>

奖励积分:闪光到白色不是瞬间而是快速并且保持原始颜色直到达到全白,然后慢慢地从白色变成新颜色,这将是理想的解决方案,但我担心它需要很多自定义动画代码。如上所述的Flash就足够了。

1 个答案:

答案 0 :(得分: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)); }
    }
}