开发者

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 fro开发者_开发问答m 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.


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜