Ive seen other similiar issues but they alwayse seem to be doing this in XAML, since this is in an event handler I need to figure out the answer in c#. basically I just need the sending menu item to blink red.
ColorAnimation ca = new ColorAnimation()
{
From = Color.FromRgb(0, 0, 0),
To = Color.FromRgb(255,0,0),
AutoReverse = true,
RepeatBehavior = new RepeatBehavior(3),
Duration=new Duration(TimeSpan.FromSeconds(.5))
};
(sender as MenuItem).Foreground.BeginAnimation(SolidColorBrush.ColorProperty, ca);
You would have to assign a mutable
SolidColorBrushinstance to the element'sForegroundproperty before it can be animated, either in XAML or in code behind:If you animate from the current color value (e.g.
Blackhere), you don't have to set theFromproperty of the animation.Note also that you shouldn't use the
asoperator without checking whether the result isnull. Better use an explicit type cast instead ofas, because in casesenderis not aMenuItem, you would correctly get anInvalidCastExceptioninstead of aNullReferenceException.