#599 – A Complete Example of a Routed Event
July 11, 2012 1 Comment
To help you understand how routed events in WPF work, it’s helpful to look at how they are implemented. Here is all of the relevant code from the ButtonBase class for the Click event.
public abstract class ButtonBase : ContentControl, ICommandSource { // Define/create the routed event object public static readonly RoutedEvent ClickEvent = EventManager.RegisterRoutedEvent("Click", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(ButtonBase)); // CLR event wrapper, adds/removes handlers public event RoutedEventHandler Click { add { AddHandler(ClickEvent, value); } remove { RemoveHandler(ClickEvent, value); } } // Method used internally to fire the Click event protected virtual void OnClick() { RoutedEventArgs newEvent = new RoutedEventArgs(ButtonBase.ClickEvent, this); RaiseEvent(newEvent); } }
Both the AddHandler and RaiseEvent methods are defined in UIElement.
Pingback: Dew Drop – July 11, 2012 (#1,361) | Alvin Ashcraft's Morning Dew