#605 – Using Subclasses of RoutedEventArgs
July 18, 2012 1 Comment
When you define your own routed event in a class, you raise the event using the UIElement.RaiseEvent method. The RaiseEvent method accepts an instance of a RoutedEventArgs object. Notice that this is same class type passed to event handlers that are declared using the RoutedEventHandler delegate type.
public delegate void RoutedEventHandler(Object sender, RoutedEventArgs e);
When firing a routed event, you can choose to use one of the subclasses of RoutedEventArgs, if you have additional information to pass back.
For example, you might declare your event to be of type MouseEventHandler and then pass back an instance of MouseEventArgs when raising the event.
public event MouseEventHandler RightDrag { add { AddHandler(RightDragEvent, value); } remove { RemoveHandler(RightDragEvent, value); } } protected virtual void OnRightDrag(MouseEventArgs e) { MouseEventArgs evargs = new MouseEventArgs(e.MouseDevice, 0); evargs.RoutedEvent = RightDragEvent; RaiseEvent(evargs); }
Pingback: Dew Drop – July 18, 2012 (1,366) | Alvin Ashcraft's Morning Dew