[英]Why e.Handled = true not working?
I have following XAML我有关注 XAML
<StackPanel MouseEnter="StackPanel_MouseEnter" Height="130" Background="Blue">
<Grid MouseEnter="Grid_MouseEnter" Height="60" Background="Red" >
<Button MouseEnter="Button_MouseEnter" Height="20"/>
</Grid>
</StackPanel>
In code behind I am doing this在后面的代码中我正在这样做
private void StackPanel_MouseEnter(object sender, MouseEventArgs e)
{
}
private void Grid_MouseEnter(object sender, MouseEventArgs e)
{
e.Handled = true;
}
private void Button_MouseEnter(object sender, MouseEventArgs e)
{
e.Handled = true;
}
Now even if I move mouse over Button
and set e.Handled = true
, the events of Grid
and StackPanel
are called respectively.现在,即使我将鼠标移到
Button
上并设置e.Handled = true
,也会分别调用Grid
和StackPanel
的事件。 Why?为什么? What should I do to stop routed event from bubbling up?
我应该怎么做才能阻止路由事件冒泡?
The MouseEnter event is not a bubbling event, it is a direct event (like classic CLR events). MouseEnter 事件不是冒泡事件,它是直接事件(如经典 CLR 事件)。 From the documentation :
从文档中:
You can define multiple MouseEnter events for objects in XAML content.
您可以为 XAML 内容中的对象定义多个 MouseEnter 事件。 However, if a child object and its parent object both define a MouseEnter event, the parent object's MouseEnter event occurs before the child object's MouseEnter event.
但是,如果子 object 及其父 object 都定义了 MouseEnter 事件,则父对象的 MouseEnter 事件将在子对象的 MouseEnter 事件之前发生。 This is not a case of a bubbling event;
这不是冒泡事件; it indicates only that the mouse (or stylus) has entered both objects, potentially at different times depending on the layout and the composition of the visual tree.
它仅表示鼠标(或触控笔)已进入这两个对象,可能在不同的时间,具体取决于视觉树的布局和组成。
So you can't prevent it from being fired on the parents.所以你不能阻止它被父母解雇。 You can use the IsMouseDirectlyOver property to see if the mouse is in fact only over the given element though.
您可以使用IsMouseDirectlyOver属性来查看鼠标实际上是否仅在给定元素上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.