How to trigger an Animation when TextBlock’s Text is changed during a DataBinding

Last time I wanted to fade out a TextBlock’s Text when the text has been changed. Unfortunately there is no TextChanged Event, but here is the solution:

<TextBlock x:Name=”tbMessage” Text=”{Binding Path=StatusBarText, NotifyOnTargetUpdated=True}”>
<TextBlock.Triggers>
<EventTrigger RoutedEvent=”Binding.TargetUpdated”>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty=”Opacity” Duration=”0:0:0″
To=”1.0″ />
<DoubleAnimation Storyboard.TargetProperty=”Opacity” Duration=”0:0:2″
From=”1.0″ To=”0.0″ BeginTime=”0:0:5″ />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</TextBlock.Triggers>
</TextBlock>

~ by Michael Scherf on February 23, 2009.

5 Responses to “How to trigger an Animation when TextBlock’s Text is changed during a DataBinding”

  1. tnx! Saved me a bit of time 🙂

    from here : http://stackoverflow.com/questions/1165862/animate-wpf-text-when-binding-updates-how

  2. nice to hear of that…but there’s one lil’ mistake: the opacity should be set to 1.0 immediately after setting a new value for the textblock.

  3. Thanks!! That was very useful.

  4. Thanks. You’ve no idea how long I was looking for a way to do this. I can’t believe how hard it is to find a concise explanation for certain things in WPF.

  5. thanks a lot

Leave a comment