Animate WPF Text when binding updates, how?

2019-02-10 20:30发布

问题:

I want to be able to create a fade animation on a text element when the binding to that element updates. In other words, the effect is that as text gets added to a text box one sees a fading effect as the text updates and then fades out. I do'nt know how to achieve this. I have seen something similar using an EventTrigger on a RoutedEvent such as FrameworkElement.Loaded but how does one detect when a binding updates?

Thanks.

回答1:

You can set properties on your binding that will cause events to be fired. There are two properties available:

  1. NotifyOnSourceUpdated: Raise the SourceUpdated event when a value is transferred from the binding target to the binding source.
  2. NotifyOnTargetUpdated: Raise the TargetUpdated event when a value is transferred from the binding source to the binding target.

From the description of your setup, it sounds like you want to use NotifyOnTargetUpdated. Set that to true in your binding, then whenever the target updates, the Binding.TargetUpdated event will fire. You can then listen to that event in an EventTrigger, and fade your text.