Show animation when databound property changess to *any* value
Is there a way to attach a trigger (and a subsequent animation) to a control which has a property databound, and have that trigger fire when the 开发者_开发知识库value of that databound property changes (to ANY value). I don't care what value it is, I just want to show a notification when the value changes to something else.
Might be very obvious, but I don't see it.
To do this entirely in XAML, you need to use an EventTrigger, but remember:
- An EventTrigger can only be used with a RoutedEvent
If the goal is to trigger off of a Binding, you can use the Binding.TargetUpdated routed event. In order for this event to be raised, you must add NotifyOnTargetUpdated=True to your binding expression. Now, every time the target of the binding expression is updated, the specified animation will play.
<TextBox Text={Binding ... NotifyOnTargetUpdated=True}>
<TextBox.Triggers>
<EventTrigger RoutedEvent="Binding.TargetUpdated">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard AutoReverse="True" BeginTime="0:0:0" Duration="0:0:0.5">
<ColorAnimation
Storyboard.TargetProperty="(TextBox.Background).(SolidColorBrush.Color)"
From="White"
To="Lavender"
/>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</TextBox.Triggers>
</TextBox>
Additionally, you can refine how and when animations are triggered by specifying specific events regardless of any bindings applied. For example, to trigger the animation only when the Text property changes, use the TextBox.TextChanged event instead.
The easiest way would be to add an extra event for this in your ViewModel that you raise manually and that you bind your EventTrigger against. You do have a ViewModel, don't you? ;)
精彩评论