Double-clicking an event fires at the window level and control level, even if it is processed inside the control

I am working on a WPF application where I have a window-level Double-click event that maximizes the application window when the user double-clicks anywhere in the window. However, I also have a custom control in the application window and a separate double-click event in the user control. When a user double-clicks in a custom control, only the double-click event should be fired, and the window should not be resized. My code looks something like this:

public class Window { private void window_DoubleClick(object sender, EventArgs e) { /// Maximize/minimize window } private void myCustomControl_DoubleClick(object sender, EventArgs e) { /// Do other things, but PLEASE don't resize! e.Handled = true; } } 

And my XAML looks something like this:

 <Window x:class="MyProject.MyWindow" MouseDoubleClick="window_DoubleClick"> <Grid Grid.Row="0" Margin="0"> <local:myCustomControl MouseDoubleClick="myCustomControl_DoubleClick"/> </Grid> </Window> 

Unfortunately, with this code in place, when the user double-clicks in the control, the control event is fired, but the Window event is also fired. I tried setting e.Handled = true in the control event, but the Window event still fires. The only thing that stopped the double-click event at the window level at startup was to handle the double-click preview using this:

 private void myCustomControl_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e) { e.Handled = true; } 

Although the preview event stops double-clicking the window before it can fire, I cannot use it without refactoring a good bit of code. My user control has several child controls that also handle the double-click event. If the preview event is fired at the parent control level, the double-click event never tunnels down through the child controls, and thus double-clicking does not do what I want it to do. If possible, I would like to avoid having to refactor any code in child controls. My question is: is there a way to stop the double-click event at the window level after the double-click event has been processed? I make myself crazy trying to figure it out. Any help is appreciated!

+5
source share
2 answers

The problem is that MouseDoubleClick only seems bubbling, but actually it doesnโ€™t. From MSDN :

Although this routed event seems to go through the barrier tree through the element tree, it is actually a direct routed event that each element tree creates with each UIElement. If you set the Handled property to true in the MouseDoubleClick event handler, subsequent MouseDoubleClick events along the route will execute with the Handled value set to false.

Instead, you can use the MouseLeftButtonDown event and check the number of clicks:

 private void myCustomControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { if (e.ClickCount == 2) { //do something e.Handled = true; } } 

Note. For some reason this does not work on MouseLeftButtonUp .

+2
source

I would try adding a check of your user control to a window-level event. Do a type check on the sender, if it matches your user control, just return it.

0
source

All Articles