【发布时间】: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