MouseDoubleClick事件不冒泡 [英] MouseDoubleClick events don't bubble

查看:477
本文介绍了MouseDoubleClick事件不冒泡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的情况下,简化了:我有一个包含员工行一个ListView,每个员工行中,有按钮增加和减少调整他的薪水

My scenario, simplified: I have a ListView containing rows of Employees, and in each Employee row, there are buttons "Increase" and "Decrease" adjusting his salary.

假装在我的程序,双击一个雇员一行意味着火这个人。

Pretend that in my program, double-clicking an Employee row means "fire this person".

问题的是,虽然我米点击增加迅速,这将触发对ListViewItem的双击事件。当然,我并不想解雇员工的时候,我只是增加他们的薪水。

The problem is that while I'm clicking "Increase" rapidly, this triggers a double click event on the ListViewItem. Naturally, I don't want to fire people when I'm just increasing their salary.

根据所有其他事件是如何工作的,我希望能够解决这个通过设置处理的= TRUE 的事件。然而,这是行不通的。这WPF生成两个独立的,完全无关联,双击事件,在我看来。

According to how all other events work, I expect to be able to solve this by setting Handled=true on the event. This, however, doesn't work. It appears to me that WPF generates two separate, completely unlinked, double click events.

下面是一个小例子,重现我的问题。可见组件:

The following is a minimal example to reproduce my issue. The visible components:

<ListView>
    <ListViewItem MouseDoubleClick="ListViewItem_MouseDoubleClick">
            <Button MouseDoubleClick="Button_MouseDoubleClick"/>
    </ListViewItem>
</ListView>

和处理程序代码:

private void Button_MouseDoubleClick(object s, MouseButtonEventArgs e) {
    if (!e.Handled) MessageBox.Show("Button got unhandled doubleclick.");
    e.Handled = true;
}

private void ListViewItem_MouseDoubleClick(object s, MouseButtonEventArgs e) {
    if (!e.Handled) MessageBox.Show("ListViewItem got unhandled doubleclick.");
    e.Handled = true;
}



发射了这个程序,双击上市按钮后的这两个的提示消息框依次显示出来。 (另外,按钮被卡在这之后向下的位置。)

After firing up this program and double-clicking the listed button, both messageboxes show up in sequence. (Also, the button is stuck in the down position after this.)

作为修复我的可以的,在ListViewItem的处理程序,检查连接到该事件的可视化树,并检查有一个按钮某处,从而放弃了事件,但这是一种不得已而为之。我想至少理解编码这种杂牌面前的问题。

As a "fix" I can, on the ListViewItem handler, inspect the visual tree attached to the event and check that "there is a button there somewhere" and thus discard the event, but this is a last resort. I want to at least understand the issue before coding such a kludge.

有谁知道的为什么的WPF做到这一点,和优雅的惯用方式避免这个问题?

Does anyone know why WPF does this, and an elegant idiomatic way to avoid the problem?

推荐答案

我想你会发现 MouseDoubleClick 事件是在的MouseDown 事件之上的抽象。也就是说,如果两个的MouseDown 事件发生在足够快的连续,在 MouseDoubleClick 事件也将得到提升。无论是按钮 ListViewItem的似乎有这样的逻辑,这样解释了为什么你看到两个截然不同的 MouseDoubleClick 事件。

I think you'll find that the MouseDoubleClick event is an abstraction on top of the MouseDown event. That is, if two MouseDown events occur in quick enough succession, the MouseDoubleClick event will also be raised. Both the Button and ListViewItem appear to have this logic, so that explains why you're seeing two distinct MouseDoubleClick events.

MSDN

虽然这个路由事件似乎
按照通过
元素树冒泡的路线,它实际上是沿各的UIElement的
元素树提出了一个直接的
路由事件。 如果您
设置Handled属性为true在
MouseDoubleClick事件处理程序,将
出现
随后MouseDoubleClick事件$沿着路径B $ B处理设置为false

Although this routed event seems to follow a bubbling route through an element tree, it actually is a direct routed event that is raised along the element tree by each UIElement. If you set the Handled property to true in a MouseDoubleClick event handler, subsequent MouseDoubleClick events along the route will occur with Handled set to false.

您可以尝试处理的MouseDown 按钮和设置,要处理,以便它不会传播到 ListViewItem的

You could try handling MouseDown on the Button and setting that to handled so that it doesn't propagate to the ListViewItem.

希望我能证实这一点我自己,但我.NET少的时刻。

Wish I could verify this myself but I'm .NET-less at the moment.

这篇关于MouseDoubleClick事件不冒泡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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