WPF TextBlock文本已更改通知

时间:2010-06-03 05:56:25

标签: wpf wpf-controls

我有一个屏幕包含大约15-20个TextBlocks,每个TextBlocks绑定到一个不同的属性,起初所有TextBlocks都是空的,文本更新来自其他客户端。

我想要做的是在文本更改时为闪烁文本设置动画3秒钟。

我使用下面的故事板来实现这一目标:

    <Setter Property="Visibility" Value="Visible"/>

       <Style.Triggers>
        <EventTrigger RoutedEvent="UIElement.MouseEnter">  

            <BeginStoryboard >
                <Storyboard Duration="0:0:03">
                    <ObjectAnimationUsingKeyFrames BeginTime="00:00:00"  Storyboard.TargetProperty="(UIElement.Visibility)">
                        <DiscreteObjectKeyFrame KeyTime="00:00:00" Value="{x:Static Visibility.Visible}"/>
                        <DiscreteObjectKeyFrame KeyTime="00:00:00.5" Value="{x:Static Visibility.Hidden}"/>
                        <DiscreteObjectKeyFrame KeyTime="00:00:01" Value="{x:Static Visibility.Visible}"/>
                        <DiscreteObjectKeyFrame KeyTime="00:00:01.5" Value="{x:Static Visibility.Hidden}"/>
                        <DiscreteObjectKeyFrame KeyTime="00:00:02" Value="{x:Static Visibility.Visible}"/>
                        <DiscreteObjectKeyFrame KeyTime="00:00:02.5" Value="{x:Static Visibility.Hidden}"/>
                        <DiscreteObjectKeyFrame KeyTime="00:00:03" Value="{x:Static Visibility.Visible}"/>
                    </ObjectAnimationUsingKeyFrames>
               </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Style.Triggers>
</Style>

使用鼠标输入事件,文本闪存很好,但使用Binding.TargetUpdated事件没有触发任何事情。

有人知道TextBlock文字发生变化时引发的事件吗?

2 个答案:

答案 0 :(得分:25)

您是否将NotifyOnTargetUpdated属性设置为true

<TextBlock Text="{Binding Path=YourProperty, NotifyOnTargetUpdated=True}" TargetUpdated="OnTargetUpdated"/>

答案 1 :(得分:6)

已经有点旧了,但这里是纯xaml中的解决方案:

<TextBlock VerticalAlignment="Center"
           Text="{Binding ErrorMsg, NotifyOnTargetUpdated=True}">
    <TextBlock.Triggers>
        <EventTrigger RoutedEvent="Binding.TargetUpdated">
            <BeginStoryboard>
                <Storyboard>
                    <DoubleAnimation BeginTime="0:0:0"
                                     Duration="0:0:1"
                                     From="0.0"
                                     Storyboard.TargetProperty="Opacity"
                                     To="1.0" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </TextBlock.Triggers>
</TextBlock>