Code Reference A collection of code for my reference (and perhaps other people too)

27Jul/090

Routed Events – Bubbling

Because I sometimes forget the syntax, a have here a quick bubbling routed event for WPF:

For you have to register the event. Then you have to create the poperty that adds and removed the handlers.

// Register the event
public static readonly RoutedEvent CloseEvent =
    EventManager.RegisterRoutedEvent("Close", RoutingStrategy.Bubble, typeof(RoutedEventHandler),
    typeof(CustomPopupControl));

// Provide CLR accessors for the event
public event RoutedEventHandler Close
{
    add { AddHandler(CloseEvent, value); }
    remove { RemoveHandler(CloseEvent, value); }
}

To manualy raise the event:

RoutedEventArgs args = new RoutedEventArgs(CloseEvent);
RaiseEvent(args);

To listen for the event:

CustomPopupControl popup = new CustomerPopupControl();
popup.Close += new RoutedEventHandler(popup_Closed);