为什么 e.Handled = true 不起作用?

发布于 2024-11-15 05:38:49 字数 769 浏览 5 评论 0原文

我在后面的 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;
}

现在,即使我将鼠标移到 Button 上并设置 e.Handled = trueGrid 的事件code> 和 StackPanel 分别被调用。为什么?我应该怎么做才能阻止路由事件冒泡?

I have following 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. Why? What should I do to stop routed event from bubbling up?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自控 2024-11-22 05:38:49

MouseEnter 事件不是冒泡事件,它是直接事件(如经典的 CLR 事件)。来自文档

可以定义多个MouseEnter
XAML 内容中的对象的事件。
但是,如果一个子对象及其
父对象都定义了一个 MouseEnter
事件,父对象的MouseEnter
事件发生在子对象之前
鼠标输入事件。这不是个案
冒泡事件;它仅表明
鼠标(或手写笔)已进入
两个对象,可能处于不同的位置
时间取决于布局和
视觉树的组成。

所以你无法阻止它被解雇给父母。您可以使用 IsMouseDirectlyOver 属性来查看鼠标是否确实在但仅在给定元素上。

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

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文