为什么两次双击都会多次引发TreeViewItem的MouseDoubleClick事件? [英] Why is the TreeViewItem's MouseDoubleClick event being raised multiple times per double click?

查看:268
本文介绍了为什么两次双击都会多次引发TreeViewItem的MouseDoubleClick事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

XAML

<TreeView Name="GroupView" ItemsSource="{Binding Documents}">
            <TreeView.ItemContainerStyle>
                <Style TargetType="{x:Type TreeViewItem}">
                    <EventSetter Event="MouseDoubleClick" Handler="OnTreeNodeDoubleClick"/>
                </Style>
            </TreeView.ItemContainerStyle>
            ....
</TreeView>

隐藏代码

private void OnTreeNodeDoubleClick(object sender, MouseButtonEventArgs mouseEvtArgs)
       {
           Console.WriteLine("{3} MouseDoubleClick Clicks={0} ChangedButton={1} Source={2} Handled={4} ButtonState={5}",
               mouseEvtArgs.ClickCount, mouseEvtArgs.ChangedButton, mouseEvtArgs.OriginalSource,
               mouseEvtArgs.Timestamp, mouseEvtArgs.Handled, mouseEvtArgs.ButtonState);
       }

我发现双击一次事件处理程序就会被多次调用.我正在尝试在相应的树节点上双击打开选项卡中的文档;所以我需要过滤掉多余的电话.

I find that for one double click, the event handler is called multiple times. I'm trying to open up a document in tab on a double-click on the corresponding tree node; so I'd need to filter out the extra calls.

23479156 MouseDoubleClick Clicks=1 ChangedButton=Left Source=System.Windows.Controls.TextBlock Handled=False ButtonState=Pressed
23479156 MouseDoubleClick Clicks=1 ChangedButton=Left Source=System.Windows.Controls.TextBlock Handled=False ButtonState=Pressed

在我稍微复杂的应用程序中,每次双击将其提高4次.在一个简单的repro-app上,每次双击将其提高2次.此外,所有事件参数的参数也相同,因此我无法区分集合中的最后一个.

In my slightly-complicated app, it is being raised 4 times per double-click. On a simple repro-app, it is being raised 2 times per double click. Also all the event argument parameters are the same too, so I can't distinguish the last one of a set.

有什么主意为什么要这样吗?

Any ideas why this is the way it is?

推荐答案

我知道这是一个老问题,但是当我在寻找解决方案的过程中碰到它时,这是我对将来任何访问者的发现!

I know this is an old question, but as I came across it in my searches for the solution, here are my findings for any future visitors!

TreeViewItem相互递归包含. TreeViewItemHeaderedContentControl(请参见 msdn ),子节点为Content.因此,每个TreeViewItem的边界都包括其所有子项.可以使用出色的 WPF检查器进行验证,方法是在可视树中选择一个TreeViewItem,它将突出显示.

TreeViewItems are recursively contained within each other. TreeViewItem is a HeaderedContentControl (see msdn), with the child nodes as the Content. So, each TreeViewItem's bounds include all of its child items. This can be verified using the excellent WPF Inspector by selecting a TreeViewItem in the visual tree, which will highlights the bounds of the TreeViewItem.

在OP的示例中,使用样式在每个TreeViewItem上注册了MouseDoubleClick事件.因此,将分别为双击的TreeViewItem及其每个父项引发该事件.可以在调试器中通过在断点双击事件处理程序中放置一个断点,并在事件args的Source属性上进行监视来验证这一点-您会注意到,每次调用事件处理程序时,它都会更改.偶然地,事件的OriginalSource保持不变.

In the OP's example, the MouseDoubleClick event is registered on each TreeViewItem using the style. Therefore, the event will be raised for the TreeViewItems that you double-clicked on - and each of its parent items - separately. This can be verified in your debugger by putting a breakpoint in your double-click event handler and putting a watch on the event args' Source property - you will notice that it changes each time the event handler is called. Incidentally, as can be expected, the OriginalSource of the event stays the same.

要应对这种意外行为,请检查Pablo在其回答中建议的选择源TreeViewItem是否对我最有效.

To counter this unexpected behaviour, checking whether the source TreeViewItem is selected, as suggested by Pablo in his answer, has worked the best for me.

这篇关于为什么两次双击都会多次引发TreeViewItem的MouseDoubleClick事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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