You don't need a Storyboard. Just assign a (modifiable) SolidColorBrush to the Label's Foreground
, and start a ColorAnimation
on its Color
property:
var colorAnimation = new ColorAnimation
{
To = Colors.Red,
Duration = TimeSpan.FromSeconds(0.1),
AutoReverse = true,
RepeatBehavior = new RepeatBehavior(3)
};
var foreground = label.Foreground as SolidColorBrush;
if (foreground == null || foreground.IsFrozen)
{
foreground = new SolidColorBrush(Colors.White);
label.Foreground = foreground;
}
foreground.BeginAnimation(SolidColorBrush.ColorProperty, colorAnimation);
You may of course have to play around with the animation's properties like Duration
, AutoReverse
and RepeatBehaviour
, in order to get the desired flashing effect.
If you assign the SolidColorBrush directly in XAML like this:
<Label x:Name="label" ...>
<Label.Foreground>
<SolidColorBrush Color="White"/>
</Label.Foreground>
</Label>
you don't need to check for it in code:
label.Foreground.BeginAnimation(SolidColorBrush.ColorProperty,
new ColorAnimation
{
To = Colors.Red,
Duration = TimeSpan.FromSeconds(0.1),
AutoReverse = true,
RepeatBehavior = new RepeatBehavior(3)
});