WPF TextBlock 文本更改通知

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2963604/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:49:49  来源:igfitidea点击:

WPF TextBlock text changed notify

wpfwpf-controls

提问by Eran

I have a screen contain about 15-20 TextBlocks each one bind to a different property, at first all the TextBlocks are empty the text update come from other client.

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

The thing I want to do is to animate flashing text for 3 seconds when ever text change.

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

I used the below storyboard to make that happen:

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

    <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>

Using the mouse enter event the text flash is fine but using the Binding.TargetUpdatedevent didn't trigger anything.

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

Anyone know about event that raise when the TextBlocktext is changed ?

有人知道TextBlock更改文本时引发的事件吗?

回答by Amsakanna

did you set the NotifyOnTargetUpdatedproperty to true

您是否将NotifyOnTargetUpdated属性设置为 true

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

回答by Rolfi

Already a bit old, but here the solution in pure xaml:

已经有点旧了,但这里是纯 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>