为什么e.Handled =真不工作? [英] Why e.Handled = true not working?

查看:498
本文介绍了为什么e.Handled =真不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下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>

在后面的代码我这样做

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;
}

现在即使我鼠标移到按钮并设置 e.Handled = TRUE 电网的事件 StackPanel中分别调用。为什么?我应该怎么做才能向上冒泡停止路由事件?

Now even if I move mouse over Button and set e.Handled = true, the events of Grid and StackPanel are called respectively. Why? What should I do to stop routed event from bubbling up?

推荐答案

MouseEnter事件不冒泡事件,这是一个直接事件(如经典的CLR事件)。从文档

The MouseEnter event is not a bubbling event, it is a direct event (like classic CLR events). From the documentation:

您可以定义在XAML内容对象多的MouseEnter
事件。
但是,如果一个子对象及其
父对象都定义一个的MouseEnter
事件,父对象的的MouseEnter
事件子对象的
MouseEnter事件之前发生。这是不冒泡事件的情况下
;它指示鼠标(或触笔)已进入$ B $仅b
两个对象,有可能在不同的
乘以取决于布局和视觉树的
组合物

You can define multiple MouseEnter events for objects in XAML content. 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. 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.

所以你不能阻止它的父母被解雇。您可以使用 IsMouseDirectlyOver 属性,看看是否鼠标实际上是在仅比虽然给定的元素。

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.

这篇关于为什么e.Handled =真不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆