Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent clicks from passing through a control to the one below it

Tags:

c#

.net

wpf

I have a textbox in a groupbox, both with double click events. When I double click in the textbox both events are triggered.

How do I stop clicks in the textbox from passing through to the groupbox? I've tried putting "e.Handled = true;" at the end of the textbox_DoubleClick event but this makes no difference.

like image 607
Skrealin Avatar asked Sep 20 '11 16:09

Skrealin


2 Answers

Because WPF uses a "tunneling / bubbling" model of event propagation, most events begin bubbling UP from the bottom of the visual tree. If you want to catch an event on the way down, there are Preview versions of the events that tunnel downwards. For example:

PreviewMouseDoubleClick

Set e.Handled = true in there.

like image 155
Dan J Avatar answered Nov 14 '22 22:11

Dan J


In your GroupBox's DoubleClick event you could check the value of e.OriginalSource and if that value is not the GroupBox, ignore the event

private void TabItem_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    if (e.OriginalSource is GroupBox)
    { 
        // Your code here
    }
}

I believe ClickEvents are actually Direct Events, and not Tunneled/Bubbled events, so setting e.Handled in one won't cancel the other.

Per MSDN Site for MouseDoubleClick

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.

like image 27
Rachel Avatar answered Nov 14 '22 23:11

Rachel