Why doesn't button click event “bubble up visual tree” to StackPanel as MSDN article states?

前端 未结 5 1628
鱼传尺愫
鱼传尺愫 2021-02-12 09:49

In the MSDN article Understanding Routed Events and Commands In WPF, it states

an event will bubble (propagate) up the visual tree from the source ele

5条回答
  •  渐次进展
    2021-02-12 10:28

    It is because all the messages are being captured handled by Button and messages stop the message stops bubbling there. The answer is right in your question's text:

    An event will bubble (propagate) up the visual tree from the source element until either it has been handled or it reaches the root element.

    EDIT:

    Edward Tanguay (OP) commented on this answer and I am copying his comment here because it is very relevant:

    "I don't see that the button IS handling the event, i.e. I have no click handler on the button, I DO have a click handler (MouseDown) on the StackPanel and hence I would think it would bubble up PAST the button since the button doesn't handle it and get handled by the stackpanel which does, right?"

    You are right. Button is not handling the MouseDown event because no handler ha been specified for it at that control.

    But, then, MouseDown is particular in some way. At least in Windows Forms it is used to initiate actions as drawing and dragging so, when a control gets the event, it proceeds to trap all the subsequent mouse messages even if you have not defined handlers for it. This trap is done when the control sets Capture property to True and this effectively stop subsequent events from being bubbled up. Capture property is set back to False by Windows Forms when it gets a MouseUp event.

    I repeat, this is the way it works in Windows Forms, you may want to double-check this but, IMHO there is no reason why this should be different for WPF.

    For reference: Se the section "Windows Forms processing" at http://blogs.msdn.com/jfoscoding/archive/2005/07/28/444647.aspx (scroll slightly down from the middle of the page).

    Note: See my comment to Arcturu's answer for a reference on bubble and tunneling events raise sequences.

提交回复
热议问题