为什么按钮单击事件“冒泡视觉树”不发生? 正如 MSDN 文章所述,到 StackPanel 了吗?
在 MSDN 文章了解 WPF 中的路由事件和命令中,它指出
事件将从源元素开始在视觉树中向上冒泡(传播),直到它被处理或到达根元素。
但是,在本例中,当您单击按钮时,它不会“冒泡视觉树”以由父 StackPanel 事件处理,即单击按钮不会触发任何事件。
为什么不? 如果不是这个,那么“冒泡”是什么意思?
XAML:
<Window x:Class="TestClickEvents456.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<StackPanel x:Name="TheStackPanel"
Background="Yellow"
MouseDown="TheStackPanel_MouseDown">
<Button x:Name="TheButton"
Margin="10"
Content="Click This"/>
<TextBlock x:Name="TheMessage"
Text="Click the button or the yellow area"/>
</StackPanel>
</Window>
代码隐藏:
using System.Windows;
using System.Windows.Input;
namespace TestClickEvents456
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void TheStackPanel_MouseDown(object sender, MouseButtonEventArgs e)
{
TheMessage.Text = "StackPanel was clicked.";
}
}
}
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 element until either it has been handled or it reaches the root element.
However, in this example, when you click the button, it doesn't "bubble up the visual tree" to get handled by the parent StackPanel event, i.e. clicking on the button fires no event.
Why not? What do they mean then by "bubbling up" if not this?
XAML:
<Window x:Class="TestClickEvents456.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<StackPanel x:Name="TheStackPanel"
Background="Yellow"
MouseDown="TheStackPanel_MouseDown">
<Button x:Name="TheButton"
Margin="10"
Content="Click This"/>
<TextBlock x:Name="TheMessage"
Text="Click the button or the yellow area"/>
</StackPanel>
</Window>
code-behind:
using System.Windows;
using System.Windows.Input;
namespace TestClickEvents456
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void TheStackPanel_MouseDown(object sender, MouseButtonEventArgs e)
{
TheMessage.Text = "StackPanel was clicked.";
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
该事件会冒泡,直到它被处理...
由于 Button 通过您的鼠标单击执行某些操作,因此它会吸收您的鼠标事件并将其转换为 ClickEvent。
如果您使用 PreviewMouseDown,您会看到 StackPanel 在按钮之前首先接收到事件。预览事件使用 Tunnel down 方法。
The event bubbles up, until it gets handled...
Since the Button does something with your mouse clicks, it absorbs your mouse event and turns it into a ClickEvent.
If you use the PreviewMouseDown, you see that the StackPanel first receives the event before the button does.. Preview events use the Tunnel down approach..
正如其他人所说,这是因为
MouseDown
事件在进一步冒泡之前由Button
处理。 您可以在 Reflector 的ButtonBase.OnMouseLeftButtonDown
中看到这一点:一种解决方案是监听
MouseDown
事件,并表明您不关心该事件是否被处理。 您可以使用AddHandler
方法来执行此操作。 它有一个布尔重载,可让您侦听已处理的事件。如果您在某处执行此操作而不是在 XAML 中设置 MouseDown 处理程序:
您将收到
TheStackPanel
上的所有MouseDown
事件,无论它们是否已被处理。As others have said, it's because the
MouseDown
event gets handled by theButton
before it can be bubbled further. You can see this in Reflector, inButtonBase.OnMouseLeftButtonDown
:One solution is to listen for a
MouseDown
event, and indicate that you don't care if the event is handled. You can do this with theAddHandler
method. It has a boolean overload that lets you listen for events which are already handled.If you do this somewhere instead of setting the MouseDown handler in XAML:
You'll receive all
MouseDown
events onTheStackPanel
, regardless of whether they've been handled.另外,如果希望stackpanel接收事件,请将stackpanel xaml更改为:
并将事件签名更改为:
在这种情况下,stackpanel将接收按钮的单击事件。 但是,单击堆栈面板本身不会触发任何事件,因为它专门监听按钮单击。
In addition, if you want the stackpanel to receive the event, change the stackpanel xaml to:
and the event signature to:
In this case, the stackpanel will recieve the button's click event. However, clicking on the stackpanel itself won't fire any event, since it listens specifically to a button click.
这是因为所有消息都被 Button 捕获
处理并且消息停止消息停止在那里冒泡。 答案就在您的问题文本中:编辑:
Edward Tanguay(OP)评论了这个答案,我在这里复制他的评论,因为它非常相关:
你是对的。 Button 未处理 MouseDown 事件,因为该控件上未为其指定处理程序。
但是,MouseDown 在某些方面是特殊的。 至少在 Windows 窗体中,它用于启动绘图和拖动等操作,因此,当控件获取事件时,即使您没有为其定义处理程序,它也会继续捕获所有后续鼠标消息。 当控件将 Capture 属性设置为 True 时,就会完成此陷阱,这有效地阻止了后续事件的冒泡。 当 Windows 窗体获取 MouseUp 事件时,Capture 属性将设置回 False。
我再说一遍,这是它在 Windows 窗体中的工作方式,您可能需要仔细检查这一点,但是,恕我直言,这对于 WPF 来说没有理由不同。
供参考:请参阅 http: //blogs.msdn.com/jfoscoding/archive/2005/07/28/444647.aspx(从页面中间稍微向下滚动)。
注意:请参阅我对 Arcturu 答案的评论,以获取有关气泡和隧道事件引发序列的参考。
It is because all the messages are being captured
handledby Button and messages stopthe message stopsbubbling there. The answer is right in your question's text:EDIT:
Edward Tanguay (OP) commented on this answer and I am copying his comment here because it is very relevant:
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.
按钮事件抑制 mousedown 和 mouseup 因为按钮事件是高级事件并且有一些代码使标志句柄为 true 这导致抑制 mousedown 来解决此问题您可以在窗口的构造函数中添加此代码
button event Suppress the mousedown and mouseup because button event is high level event and have bit of code that give flag handle true this cause Suppressed for mousdown to resolve this problem you can add this code in constructor of window