Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double-click event fires at window level and control level, even when it is handled within the control

Tags:

c#

events

wpf

I am working on a WPF application where I have a Window level double-click event that maximizes the app window when a user double-clicks anywhere within the window. However, I also have a custom control within the app window, and a separate double-click event within the custom control. When a user double-clicks in the custom control, only the control's double-click event should fire, and the window should not resize. 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 fires, but the Window event fires as well. I have tried setting e.Handled=true in the control event, but the Window event fires anyway. The only thing that has stopped the Window level double-click event from firing is handling the custom control's preview double-click event using this:

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

Though the preview event stops the Window double-click before it can fire, I can't use it without having to refactor a good bit of code. My custom control has a few child controls that also handle its double-click event. If the preview event gets called at the parent control level, the double-click event never tunnels down through the child controls, and thus double-clicking doesn't do what I want it to do. If possible, I would like to avoid having to refactor any of the code in the child controls. That being said, my question is this: Is there any way to stop the Window-level double-click event from firing once the control's double-click event has been handled? I've been driving myself crazy trying to figure this out.. any help is appreciated!

like image 618
chelsea Avatar asked Nov 01 '22 02:11

chelsea


1 Answers

The problem is, that MouseDoubleClick only appears to bubble, but really doesn't. From MSDN:

Although this routed event seems to follow a bubbling route through an element tree, it actually is a direct routed event that is raised along the element tree by each UIElement. If you set the Handled property to true in a MouseDoubleClick event handler, subsequent MouseDoubleClick events along the route will occur with Handled set to false.

Instead you could use the MouseLeftButtonDown event and check the clickcount:

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

Note: This does not work on MouseLeftButtonUp for some reason.

like image 197
Tim Pohlmann Avatar answered Nov 15 '22 05:11

Tim Pohlmann