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

前端 未结 3 1190
挽巷
挽巷 2021-01-18 13:46

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

相关标签:
3条回答
  • 2021-01-18 14:25

    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.

    0 讨论(0)
  • 2021-01-18 14:28

    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.

    0 讨论(0)
  • 2021-01-18 14:48

    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 讨论(0)
提交回复
热议问题