I have a View Model object that has a depednency property called IsSearching; it is a bool. My form has a TextBlock control that is used to display the number of matching rows. The property that is bound to the TextBlock's Text property is an int? type. At the start of the search, the property is set to null. The converter in the binding displays this as “–“.
I want the “–” string to flash while the IsSearching property is true, and I don’t want it flash when it is false. Here’s what I have so far:
The StoryBoard:
<Storyboard x:Key="FlashTextBlockText">
<ObjectAnimationUsingKeyFrames RepeatBehavior="Forever" Storyboard.TargetProperty="(UIElement.Visibility)">
<DiscreteObjectKeyFrame KeyTime="0:0:0.5" Value="{x:Static Visibility.Hidden}" />
<DiscreteObjectKeyFrame KeyTime="0:0:1" Value="{x:Static Visibility.Visible}" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
A named Style for TextBlocks only:
<Style BasedOn="{StaticResource {x:Type TextBlock}}" x:Key="FlashBlockText" TargetType="TextBlock">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsSearching}" Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource FlashTextBlockText}" />
</DataTrigger.EnterActions>
</DataTrigger>
</Style.Triggers>
</Style>
The IsSearching property is initially false. When the user clicks on the Search Button, it is set to true and the flashing begins. But it doesn’t stop flashing.
How do I get the StoryBoard to stop when IsSearching is set back to false?
Tony
Triggers operating on property values have two collections containing actions:
EnterActions: Actions in this collection are activated when the trigger itself is activated (in your example when IsSearching becomes true)
ExitActions: Actions in here are activated when the trigger is deactivated (IsSearching becomes false)
So you should just stop the storyboard by adding a StopStoryboard action inside the ExitActions collection.
There is no need to create a new DataTrigger where you explicitly check whether the property has become false.