Warren.Tang1

Hi,

Here is the code:

Code Block

<Grid
MouseLeftButtonDown="MouseDownGrid"
PreviewMouseLeftButtonDown="PreviewMouseDownGrid"
Width="313"
Height="271"
>

<Button
PreviewMouseLeftButtonDown="PreviewMouseDownButton"
MouseLeftButtonDown="MouseDownButton"
Click="MyClickEvent"
Name="btnGo"
>

<TextBox
MouseLeftButtonDown="MouseLeftButtonDownTextBox"
PreviewMouseLeftButtonDown="PreviewMouseLeftButtonDownTextBox"
Width="173"
Height="27"
Name="textBox1"
>
</TextBox>

</Button>

</Grid>

All event handlers only have a single line like this:

Debug.WriteLine("PreviewMouseDownButton");

The expected result should be:

PreviewMouseDownGrid
PreviewMouseDownButton
PreviewMouseLeftButtonDownTextBox

MouseLeftButtonDownTextBox

MouseDownButton

MouseDownGrid

However I could only get:

PreviewMouseDownGrid
PreviewMouseDownButton
PreviewMouseLeftButtonDownTextBox

Why did the event not bubble up

Regards




Re: Windows Presentation Foundation (WPF) Event bubbling does not work

Tim Dawson

Because the textbox handles the event. If the event is handled, it stops bubbling.






Re: Windows Presentation Foundation (WPF) Event bubbling does not work

LesterLobo - MSFT

disaling the textbox would not handle the event and you should see the event bubbling





Re: Windows Presentation Foundation (WPF) Event bubbling does not work

Warren Tang

Thanks for your replies. It seems that the enabled textbox does not bubble up mouse events.

As I made some changes to my code, I found that only the click event bubbles up, while the mouse down/up events just do not though they can be previewed.

Code Block

<Button Name="btnTop" Width="300" Height="300"
Tag="Top "

PreviewMouseLeftButtonDown="MyRoutedEventHandler"
PreviewMouseDown="MyRoutedEventHandler"
PreviewMouseUp="MyRoutedEventHandler"
PreviewMouseLeftButtonUp="MyRoutedEventHandler"

MouseLeftButtonDown="MyRoutedEventHandler"
MouseDown="MyRoutedEventHandler"
Click="MyRoutedEventHandler"
MouseDoubleClick="MyRoutedEventHandler"
MouseUp="MyRoutedEventHandler"
MouseLeftButtonUp="MyRoutedEventHandler"
>

<Button Name="btnMiddle" Width="200" Height="100"
Tag="Middle"

PreviewMouseLeftButtonDown="MyRoutedEventHandler"
PreviewMouseDown="MyRoutedEventHandler"
PreviewMouseUp="MyRoutedEventHandler"
PreviewMouseLeftButtonUp="MyRoutedEventHandler"

MouseLeftButtonDown="MyRoutedEventHandler"
MouseDown="MyRoutedEventHandler"
Click="MyRoutedEventHandler"
MouseDoubleClick="MyRoutedEventHandler"
MouseUp="MyRoutedEventHandler"
MouseLeftButtonUp="MyRoutedEventHandler"
>

<Button Name="btnBottom" Width="150" Height="30" Background="Aqua" Content=" Click Here!"
Tag="Bottom"

PreviewMouseLeftButtonDown="MyRoutedEventHandler"
PreviewMouseDown="MyRoutedEventHandler"
PreviewMouseUp="MyRoutedEventHandler"
PreviewMouseLeftButtonUp="MyRoutedEventHandler"

MouseLeftButtonDown="MyRoutedEventHandler"
MouseDown="MyRoutedEventHandler"
Click="MyRoutedEventHandler"
MouseDoubleClick="MyRoutedEventHandler"
MouseUp="MyRoutedEventHandler"
MouseLeftButtonUp="MyRoutedEventHandler"
>
</Button>

</Button>

</Button>

Code Block

private void MyRoutedEventHandler(object sender, RoutedEventArgs e)
{
Debug.WriteLine(string.Format("{0:mm:ss}\t{1}\t{2}", DateTime.Now,((Button)sender).Tag, e.RoutedEvent.Name));
}

I got the following sequence:

Code Block
08:00 Top PreviewMouseLeftButtonDown
08:00 Top PreviewMouseDown
08:00 Middle PreviewMouseLeftButtonDown
08:00 Middle PreviewMouseDown
08:00 Bottom PreviewMouseLeftButtonDown
08:00 Bottom PreviewMouseDown
08:00 Top PreviewMouseLeftButtonUp
08:00 Top PreviewMouseUp
08:00 Middle PreviewMouseLeftButtonUp
08:00 Middle PreviewMouseUp
08:00 Bottom PreviewMouseLeftButtonUp
08:00 Bottom PreviewMouseUp
08:00 Bottom Click
08:00 Middle Click
08:00 Top Click

Certainly we can suppress the event tunneling&bubbling by e.Handled = true, however can we let the mouse up/down events bubble up The answer seems to be negative but I just want to make sure. Thanks.

Regards






Re: Windows Presentation Foundation (WPF) Event bubbling does not work

Carole Snyder

You always have the option of receiving an event handler when e.Handled = true. You just need to attach the event by calling this overload of UIElement.AddHandler and passing handledEventsToo as true.






Re: Windows Presentation Foundation (WPF) Event bubbling does not work

Warren Tang

Thank you! Carole.

I added this to my code:

Code Block

btnTop.AddHandler(Button.MouseLeftButtonDownEvent, new RoutedEventHandler(MyRoutedEventHandler), true);
btnTop.AddHandler(Button.MouseDownEvent, new RoutedEventHandler(MyRoutedEventHandler), true);
btnTop.AddHandler(Button.MouseUpEvent, new RoutedEventHandler(MyRoutedEventHandler), true);
btnTop.AddHandler(Button.MouseLeftButtonUpEvent, new RoutedEventHandler(MyRoutedEventHandler), true);

btnMiddle.AddHandler(Button.MouseLeftButtonDownEvent, new RoutedEventHandler(MyRoutedEventHandler), true);
btnMiddle.AddHandler(Button.MouseDownEvent, new RoutedEventHandler(MyRoutedEventHandler), true);
btnMiddle.AddHandler(Button.MouseUpEvent, new RoutedEventHandler(MyRoutedEventHandler), true);
btnMiddle.AddHandler(Button.MouseLeftButtonUpEvent, new RoutedEventHandler(MyRoutedEventHandler), true);

btnBottom.AddHandler(Button.MouseLeftButtonDownEvent, new RoutedEventHandler(MyRoutedEventHandler), true);
btnBottom.AddHandler(Button.MouseDownEvent, new RoutedEventHandler(MyRoutedEventHandler), true);
btnBottom.AddHandler(Button.MouseUpEvent, new RoutedEventHandler(MyRoutedEventHandler), true);
btnBottom.AddHandler(Button.MouseLeftButtonUpEvent, new RoutedEventHandler(MyRoutedEventHandler), true);

Finally I caught them all:

Code Block
22:49 Top PreviewMouseLeftButtonDown
22:49 Top PreviewMouseDown
22:49 Middle PreviewMouseLeftButtonDown
22:49 Middle PreviewMouseDown
22:49 Bottom PreviewMouseLeftButtonDown
22:49 Bottom PreviewMouseDown
22:49 Bottom MouseLeftButtonDown
22:49 Bottom MouseDown
22:49 Middle MouseLeftButtonDown
22:49 Middle MouseDown
22:49 Top MouseLeftButtonDown
22:49 Top MouseDown
22:49 Top PreviewMouseLeftButtonUp
22:49 Top PreviewMouseUp
22:49 Middle PreviewMouseLeftButtonUp
22:49 Middle PreviewMouseUp
22:49 Bottom PreviewMouseLeftButtonUp
22:49 Bottom PreviewMouseUp
22:49 Bottom Click
22:49 Middle Click
22:49 Top Click
22:49 Bottom MouseLeftButtonUp
22:49 Bottom MouseUp
22:49 Middle MouseLeftButtonUp
22:49 Middle MouseUp
22:49 Top MouseLeftButtonUp
22:49 Top MouseUp

I hope these two round trips will benefit those who are learning event tunneling/bubbling.

Thanks again for your help.

Regards







Re: Windows Presentation Foundation (WPF) Event bubbling does not work

Tony.squirrel

Hi,I also do like that, why get the error "No overload for 'MyRoutedEventHandler' matches delegate 'System.Windows.RoutedEventHandler" Thanks for your help.





Re: Windows Presentation Foundation (WPF) Event bubbling does not work

Tony.squirrel

I'm sorry, I made some mistakes. I using MouseButtonEventArgs type replace RoutedEventArgs, it's no matched. Also I find a strange thing, I use MessageBox to display the message, can't catch Click event and bubbling event(of course I add the AddHandler code).