I am trying to make a custom control in WPF. I want it to simulate the behavior of a LED that can blink.
There are three states to the control: On, Off, and Blinkin
To allow for greater control of the blink rate and such in your code behind, I'd suggest having a routed event in your UserControl called Blink:
public static readonly RoutedEvent BlinkEvent = EventManager.RegisterRoutedEvent("Blink", RoutingStrategy.Direct, typeof(RoutedEventHandler), typeof(LedControl));
public event RoutedEventHandler Blink
{
add { AddHandler(BlinkEvent, value); }
remove { RemoveHandler(BlinkEvent, value); }
}
In your code behind you can set up a timer to raise the event however often you like (this also gives you the opportunity to blink the light a single time whenever you want:
RaiseEvent(new RoutedEventArgs(LedControl.Blink));
Now in XAML, the following code would make a glow visible, and set the fill property of your ellipse (ledEllipse) to a bright green radial gradient, then return the fill value to a dim 'unlit' green (which you could change to gray if you like). You can simply change the duration to make the blink last longer.
Also, I am directly referencing the ellipse 'ledEllipse' and it's corresponding DropShadowEffect 'glow' which are defined in the ledControl as following (redLight is just another radial gradient brush that I start my led's fill property at):
Note: The DropShadowEffect was introduced in .Net 3.5, but you could remove that if you don't want a glow effect (but it looks nice on a solid color contrasting background).