【问题标题】:WPF flash when fill/background color changes填充/背景颜色更改时 WPF 闪烁
【发布时间】:2016-03-27 12:13:25
【问题描述】:

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

<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。颜色变化的检测部分效果很好,但是闪光部分(显然)不能像我想的那样工作:它首先将填充颜色更改为新颜色,然后慢慢变白,最后变回新颜色。 如上所述,目标是“闪光”——瞬间变白,然后恢复新颜色。
请注意,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 就足够了。

【问题讨论】:

    标签: c# wpf colors


    【解决方案1】:

    只需从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)); }
        }
    }
    

    【讨论】:

    • 就这么简单?我真的花了一天时间解决这个问题......非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-24
    • 1970-01-01
    • 2021-06-29
    • 1970-01-01
    • 2013-04-17
    • 1970-01-01
    • 2011-11-24
    相关资源
    最近更新 更多