Alex Rivera | Logout

Stumped - bubbling an routedevent up to top window in WPF - how to capture anywhere!

Asked 2010-03-04T12:53:36.633
10

The literature is awful in this regard - all I want to do is bubble an event up from a control that I have defined but created on the fly (hopefully this will not cause issues) from a broker controller class. The control is a PopUp.

public static readonly RoutedEvent weClosed = EventManager.RegisterRoutedEvent("TIMBOO", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(FixedTaskbarNotifier));


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

    // This method raises the Tap event
    void RaiseTapEvent()
    {
        RoutedEventArgs newEventArgs = new RoutedEventArgs(weClosed);
        RaiseEvent(newEventArgs);}


    protected override void OnClosed(EventArgs e)
    {
        //TO DO - rearrange current open windows - fire event?
        Log.Instance.Info("Clean up Window");
        RaiseTapEvent(); This is called on close but nothing fires ..
                }

I am hoping to catch the event somewhere - even in my parent window or higher up the visual tree. This has a reference to a controller that holds the list of notification controls -once I know which one was closed, I can reoganise / repaint any others still active via the controller, but how to catch? Thanks!

Edit
Report

1 Answer

4

On your parent, or wherever you want to handle it, you should simply be able to handle the event by declaring what class it is declared on and the event name. In your case, on your window declaration, do:

<Window ... FixedTaskbarNotifier.TIMBOO="<EventHandlerName>">

This will catch all events bubbled up that have not been canceled (and you can even get those, if you want, although I believe that is not considered a good practice).

As a simple example, create a window and place a button on it. On the window, add a handler for the click operation (which is defined on ButtonBase)

<Window ... ButtonBase.Click="Window_Click">

This will then fire any time any child button is clicked, even inside of child controls and such, unless the event is intentionally canceled.

answered 2010-03-04T13:19:24.633

Your Answer