views:

197

answers:

1

I have a WPF window, which has a button that is inside a stackPanel, which is inside another stackPanel

I wrote an event handler for the button for the MouseDown event. I want to execute this eventHandler three times for the button and the parent (stack panel) and the parent's parent

How can I achieve that with the routed event, by writing only one event handler? I don't want to repeat the event handler code.

Thanks

A: 
  1. Implement a method to be called from the event handlers of the Button and StackPanel's.
  2. Sets the Handled property of the args to false.
  3. Or you can have a Boolean parameter in your GenericHandler method so that you can decide whether it should let the event bubble.

    void GenericHandler(object sender, RoutedEventArgs args)
    

    { // Check for the type of the args here and do your work.

    args.Handled = false; // this lets the event bubbled up.
    ...
    

    }

mqbt