I have been trying for some time to animate a TextBlock background from white to red and back again (blinking) when a data bound boolean property is set to 'True'. Every time I run it, it goes into break mode.
I know I am missing something. Any help or direction that you can provide would be so very appreciated.
Thank you in advance!
<TextBlock x:Name="txtItemDisplayText" HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding ItemCountText}" VerticalAlignment="Top" Height="600" Width="800" TextAlignment="Center" FontSize="525" FontWeight="Bold" FontFamily="Agency FB" Foreground="Green">
<TextBlock.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding Blink}" Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard Name="sbBlink">
<Storyboard>
<ColorAnimation Storyboard.TargetProperty="(TextBlock.Background).(SolidColorBrush.Color)"
From="White" To="Red" FillBehavior="Stop"
BeginTime="0:0:0" Duration="0:0:0.3"/>
<ColorAnimation Storyboard.TargetProperty="(TextBlock.Background).(SolidColorBrush.Color)"
From="Red" To="White"
BeginTime="0:0:0.3" Duration="0:0:1"/>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
<DataTrigger.ExitActions>
<RemoveStoryboard BeginStoryboardName="sbBlink"/>
</DataTrigger.ExitActions>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
Private mBlink As Boolean = False
Public Property Blink() As Boolean
Get
Return mBlink
End Get
Set(value As Boolean)
mBlink = value
RaisePropertyChanged("Blink")
End Set
End Property
You can't animate the Color property of the predefined Brushes. Add a Setter for the Background property that explicitly sets a new SolidColorBrush instance:
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Background">
<Setter.Value>
<SolidColorBrush Color="White"/>
</Setter.Value>
</Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding Blink}" Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard Name="sbBlink">
<Storyboard>
<ColorAnimation Storyboard.TargetProperty="Background.Color"
From="White" To="Red" FillBehavior="Stop"
BeginTime="0:0:0" Duration="0:0:0.3"/>
<ColorAnimation Storyboard.TargetProperty="Background.Color"
From="Red" To="White"
BeginTime="0:0:0.3" Duration="0:0:1"/>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
<DataTrigger.ExitActions>
<RemoveStoryboard BeginStoryboardName="sbBlink"/>
</DataTrigger.ExitActions>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>