【问题标题】:WPF TextBlock animation not firing when source is updated更新源时未触发 WPF TextBlock 动画
【发布时间】:2026-02-17 01:40:01
【问题描述】:

我只是想让一些文本在此 TextBlock 的源更新时瞬间闪烁红色。 TextBlock 绑定的文本可以正常工作,但由于某种原因动画不会触发。我有点不知所措。

有什么想法吗?

            <Border BorderBrush="{StaticResource Button.BackgroundBrush}"
                    Background="{StaticResource Screener.Background}"
                    BorderThickness="0,1,0,0"
                    HorizontalAlignment="Stretch"
                    VerticalAlignment="Stretch">
                <TextBlock Text="{Binding AlertBoxMessage, Mode=OneWay, NotifyOnSourceUpdated=True}"
                           Name="AlertBox"
                           MinHeight="55"
                           FontWeight="Normal"
                           FontSize="16"
                           HorizontalAlignment="Center"
                           VerticalAlignment="Center"
                           Foreground="Black">
                    <TextBlock.Style>
                        <Style TargetType="{x:Type TextBlock}">
                            <Style.Triggers>
                                <EventTrigger RoutedEvent="Binding.SourceUpdated" >
                                    <BeginStoryboard>
                                        <Storyboard Storyboard.TargetProperty="Foreground">
                                            <ColorAnimation From="Black"
                                                    To="Red"
                                                    AutoReverse="True" 
                                                    RepeatBehavior="3"
                                                    Duration="0:0:2"/>
                                        </Storyboard>
                                    </BeginStoryboard>
                                </EventTrigger>
                            </Style.Triggers>
                        </Style>
                    </TextBlock.Style>
                </TextBlock>
            </Border>

【问题讨论】:

标签: wpf xaml


【解决方案1】:

首先,我认为您误解了Binding.SourceUpdated 事件。

每个绑定都包含两个端点——一个源和一个目标。目标是设置绑定的对象,目标属性是将绑定其值的DependencyProperty,源是将解析Binding.Path的对象,源属性是目标属性的属性是绑定的。在您的情况下,目标是TextBlock,目标属性是Text,源是(继承的)数据上下文(我假设的视图模型),源属性是AlertBoxMessage

您可能知道绑定可以在多种模式下工作。 TwoWay 将允许在源到目标和目标到源的方向上进行更新,而 OneWay 将只进行源到目标的更新。

这里的关键信息是,只要发生目标到源的传输,就会引发Binding.SourceUpdated(对于源到目标的传输,Binding.TargetUpdated,请参阅MSDN)。但是,在您的情况下,该值始终在源到目标的方向上更新,因为您将绑定模式设置为OneWay,因此永远不会引发Binding.SourceUpdated。为了实现您的目标,您应该改用Binding.TargetUpdated 事件(将Binding.NotifyOnTargetUpdated 设置为true)。

但是,您的代码还有其他几个问题会阻止它工作,所以让我们来看看它们:

  1. 您正在尝试使用ColorAnimationTextBlock.Foreground 属性(Brush)设置动画,该属性只能用于为Color 类型的属性设置动画,您将获得@987654342 @。你应该设置StoryBoard.TargetProperty="Foreground.Color"
  2. 这似乎有点违反直觉,但设置RepeatBehavior="3" 将使动画重复3 天,而不是3 次(参见this question)。你应该使用RepeatBehavior="3x"

总而言之,这里的代码应该可以满足您的期望:

<TextBlock Text="{Binding AlertBoxMessage, Mode=OneWay, NotifyOnTargetUpdated=True}"
           Name="AlertBox"
           MinHeight="55"
           FontWeight="Normal"
           FontSize="16"
           HorizontalAlignment="Center"
           VerticalAlignment="Center"
           Foreground="Black">
    <TextBlock.Style>
        <Style TargetType="{x:Type TextBlock}">
            <Style.Triggers>
                <EventTrigger RoutedEvent="Binding.TargetUpdated">
                    <BeginStoryboard>
                        <Storyboard Storyboard.TargetProperty="Foreground.Color">
                            <ColorAnimation From="Black"
                                            To="Red"
                                            AutoReverse="True" 
                                            RepeatBehavior="3x"
                                            Duration="0:0:2"/>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>

【讨论】:

  • 谢谢。你的帖子非常有帮助。在这一点上,我只是为了好玩而这样做,所以我对此很陌生。有时我只是在思考我是在处理目标/源还是我想要我的绑定模式 oneway 还是 onewaytosource。我也忘了考虑刷子而不是颜色,但至少我可以弄清楚……一旦它坏了。