【发布时间】:2017-11-01 11:26:34
【问题描述】:
当绑定到文本框的 Text 属性的值发生更改时,我正在尝试为边框的颜色设置动画:
<Border x:Name="border" BorderThickness="3" BorderBrush="Blue" HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock Background="White" TextAlignment="Center" FontSize="24"
Text="{Binding Value, NotifyOnTargetUpdated=True}">
<TextBlock.Triggers>
<EventTrigger RoutedEvent="Binding.TargetUpdated">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation To="Yellow"
Storyboard.TargetName="border"
AutoReverse="True"
Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)"
FillBehavior="Stop"
Duration="0:0:0.5"
RepeatBehavior="5x" />
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</TextBlock.Triggers>
</TextBlock>
</Border>
问题是即使在应用程序启动并且值尚未设置一次时也会触发动画。 有没有办法避免这种情况?
【问题讨论】:
-
所有这些都绑定到一个视图模型吗?
-
您可以在加载 TextBlock 后或首次引发 Binding.TargetUpdated 后以编程方式添加触发器。
-
@BradleyUffner 是的,它是一个实现 INotifyPropertyChanged 的简单视图模型
-
你可以触发你控制的其他东西的动画。我知道这并不理想,但你可以有一个
bool属性来确定动画是否应该播放,触发该属性为true的动画,然后在@987654325 的设置器中将该属性设置为true@。我正在寻找“更好”的选项,但 WPF 动画是一个我还没有太多经验的领域。
标签: c# wpf wpf-animation