【问题标题】:WPF StopStoryboard - after animation stops it does not Bind to the Color property [duplicate]WPF StopStoryboard - 动画停止后它不会绑定到颜色属性[重复]
【发布时间】:2021-02-19 04:25:10
【问题描述】:

我是 WPF 的新手,我有一个矩形,如果某个布尔值为真,我会尝试将其动画为肉红色。我希望它在布尔值为假时停止。为此,我使用了<DataTrigger.ExitActions> 但是,我仍然希望我的填充颜色根据 AlertColor 更改,但是在动画停止后,绑定似乎也停止了,并且背景颜色仅保持 LightPink。 为什么?我该如何解决这个问题,是否有更好的方法仅在特定颜色的情况下为颜色设置动画,并在颜色更改时停止动画(使用绑定)?

XAML 相关代码:

 <Rectangle  Width="840" Height="40">
                    <Rectangle.Style>
                        <Style TargetType="Rectangle">
                            <Setter Property="Fill">
                                <Setter.Value>
                                    <SolidColorBrush Color="{Binding AlertUnit.AlertColor , FallbackValue=LightPink}"/>
                                </Setter.Value>
                            </Setter>
                            <Style.Triggers>
                                <DataTrigger   Binding="{Binding AlertUnit.Emergency}"  Value="true" >
                                   <DataTrigger.EnterActions>
                                      <BeginStoryboard Name="FlashingRedAnimation">
                                          <Storyboard>
                                              <ColorAnimation Storyboard.TargetProperty="Fill.Color" To="White" Duration="0:0:1" AutoReverse="True"
                                                              RepeatBehavior="Forever"></ColorAnimation>
                                          </Storyboard>
                                      </BeginStoryboard>
                                   </DataTrigger.EnterActions>
                                    <DataTrigger.ExitActions>
                                        <StopStoryboard BeginStoryboardName="FlashingRedAnimation" />
                                    </DataTrigger.ExitActions>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </Rectangle.Style>
                </Rectangle>
  • 编辑:

我发现这个问题真的很愚蠢。绑定应该是:

  Binding AlertUnit.AlertColor.Color

不是:

Binding AlertUnit.AlertColor

感谢大家的帮助。

【问题讨论】:

  • 如果颜色停留在LightPink,说明你绑定AlertUnit.AlertColor失败。这个属性的值是什么,它是如何定义的,你如何以及何时设置它?
  • 考虑将AlertColor 声明为颜色而不是画笔。您已经知道 AlertColor 并不是一个非常棒的画笔名称。
  • @Clemens 使用颜色而不是画笔有什么好处/区别?我继续开发一个现有项目,在这个项目中,所有具有某种逻辑的 UI 颜色都是画笔,所以我在这个新模型中做了同样的事情。 (第一次使用 C# & WPF)
  • 然后至少给它起一个合理的名字,例如AlertBrush。任何看到AlertColor 的人都会认为它是一种颜色,包括您自己,正如这个问题所表明的那样。
  • 除了名称之外,使用Brush而不是Color有什么缺点吗?

标签: c# wpf xaml user-interface


【解决方案1】:

我通过动画画笔的不透明度值而不是颜色来产生类似的视觉效果。

<Style TargetType="{x:Type vctrl:perBlinkingBorder}">
    <Style.Triggers>
        <Trigger Property="IsBlinking" Value="True">
            <Trigger.EnterActions>
                <BeginStoryboard>
                    <Storyboard TargetProperty="(BlinkingBorderBrush).Opacity">
                        <DoubleAnimation
                            AutoReverse="True"
                            RepeatBehavior="Forever"
                            From="1"
                            To="0"
                            Duration="0:0:0.5">
                        </DoubleAnimation>
                    </Storyboard>
                </BeginStoryboard>
            </Trigger.EnterActions>
        </Trigger>
    </Style.Triggers>
</Style>

为了保证去除视觉效果,我在perBlinkingBorder控件上设置了两个画笔属性,在设置IsBlinking属性时进行切换。

关于我最近的blog post 的更多详细信息和演示项目。

【讨论】:

    【解决方案2】:

    我发现这个问题真的很愚蠢。 绑定应该是:

      Binding AlertUnit.AlertColor.Color
    

    不是:

    Binding AlertUnit.AlertColor
    

    谢谢大家的帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-10
      • 2010-10-05
      • 2012-06-20
      • 1970-01-01
      • 1970-01-01
      • 2011-01-12
      相关资源
      最近更新 更多