如何禁用在WPF的TreeView双击行为? [英] How to disable double click behaviour in a WPF TreeView?

查看:246
本文介绍了如何禁用在WPF的TreeView双击行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的的TreeView ,我有不同的事件的MouseDown / 的MouseUp 等,但如果我这样做不够快的TreeView 展开/折叠树节点。我不想这样烤的行为。

In my TreeView, I have different events for MouseDown/MouseUp, etc but when I do it fast enough the TreeView expands/collapses the TreeNode. I don't want this baked-in behaviour.

有没有一种方法来禁用此?

Is there a way to disable this?

推荐答案

您可以燮preSS树型视图的双击事件像这样:

You could suppress the double click event of TreeViewItem like so:

XAML:

<TreeView DockPanel.Dock="Left" TreeViewItem.PreviewMouseDoubleClick="TreeViewItem_PreviewMouseDoubleClick">
    <TreeViewItem Header="Node Level 1" IsExpanded="True" >
        <TreeViewItem Header="Node Level 2.1" >
            <TreeViewItem Header="MyItem" />
        </TreeViewItem>
        <TreeViewItem Header="Node Level 2.2">
            <TreeViewItem Header="MyItem" />
        </TreeViewItem>
    </TreeViewItem>
</TreeView>

code:

code:

private void TreeViewItem_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    //this will suppress the event that is causing the nodes to expand/contract 
    e.Handled = true;
}

更新

据MSDN <一个href="http://msdn.microsoft.com/en-us/library/system.windows.controls.control.$p$pviewmousedoubleclick.aspx">docs:

虽然这个路由事件似乎   遵循通过隧道路线   元素树,它实际上是一种直接   即沿凸起路由事件   由每个UIElement的元素树...   谁想要处理控制作者   鼠标双击应该使用   previewMouseLeftButtonDown事件时   点击次数等于二。这会   导致国家的处理,以   在的情况下传播适当   其中,在元件的另一元件   树处理该事件。

Although this routed event seems to follow a tunneling route through an element tree, it actually is a direct routed event that is raised along the element tree by each UIElement... Control authors who want to handle mouse double clicks should use the PreviewMouseLeftButtonDown event when ClickCount is equal to two. This will cause the state of Handled to propagate appropriately in the case where another element in the element tree handles the event.

我不知道这为什么你有问题或没有,但我们会做到这一点的MSDN的方式,并使用 previewMouseLeftButtonDown 来代替:

I'm not sure if this why you are having issues or not, but we'll do it the MSDN way and use PreviewMouseLeftButtonDown instead:

XAML:

<TreeView DockPanel.Dock="Left" TreeViewItem.PreviewMouseLeftButtonDown="TreeView_PreviewMouseLeftButtonDown">
    <TreeViewItem Header="Node Level 1" IsExpanded="True">
        <TreeViewItem Header="Node Level 2.1" >
            <TreeViewItem Header="MyItem" />
        </TreeViewItem>
        <TreeViewItem Header="Node Level 2.2">
            <TreeViewItem Header="MyItem" />
        </TreeViewItem>
    </TreeViewItem>
</TreeView>

code:

code:

private void TreeView_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    if (e.ClickCount > 1)
    {
        //here you would probably want to include code that is called by your
        //mouse down event handler.
        e.Handled = true;
    }
}

我测试过这一点,它的作品无论我有多少次点击

I've tested this and it works no matter how many times i click

这篇关于如何禁用在WPF的TreeView双击行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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