Skrealin
Skrealin

Reputation: 1114

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

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.

Upvotes: 7

Views: 6774

Answers (3)

Dan J
Dan J

Reputation: 16708

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.

Upvotes: 10

Rachel
Rachel

Reputation: 132558

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.

Upvotes: 5

Bek Raupov
Bek Raupov

Reputation: 3777

you should handle e.Handled in the PreviewDoubleClick because tunneled events happens before bubbled up ones.

also why would you need to handle that event in both textbox and groupbox ? as it is getting fired in both because 2 separate events are getting fired.

Upvotes: 1

Related Questions