如何禁用 WPF TreeView 中的双击行为?

发布于 2024-11-08 17:10:41 字数 186 浏览 0 评论 0原文

在我的 TreeView 中,我有不同的 MouseDown/MouseUp 事件,等等,但是当我做得足够快时,TreeView > 展开/折叠TreeNode。我不想要这种固有的行为。

有办法禁用这个吗?

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?

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

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

发布评论

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

评论(2

我不在是我 2024-11-15 17:10:41

您可以抑制 TreeViewItem 的双击事件,如下所示:

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:

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

UPDATE

根据 msdn 文档

虽然这个路由事件似乎
沿着隧道路线穿过
元素树,它实际上是一个直接的
沿着引发的路由事件
元素树由每个 UIElement...
控制想要处理的作者
鼠标双击应该使用
PreviewMouseLeftButtonDown 事件时
ClickCount 等于二。这将
导致 Handled 的状态变为
在这种情况下适当传播
其中元素中的另一个元素
树处理该事件。

我不确定这是否是您遇到问题的原因,但我们将按照 MSDN 方式进行操作并使用 PreviewMouseLeftButtonDown 代替:

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:

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

我已经对此进行了测试,无论如何它都有效我点击了多少次

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:

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

UPDATE

According to msdn docs:

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.

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:

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

变身佩奇 2024-11-15 17:10:41

如果您想防止 DoubleClick 上的 TreeView 展开/折叠,但同时为此事件使用命令,则可以使用以下解决方案:

https://stackoverflow.com/a/60869105/1206431

If you want to prevent the expanding/collapsing of the TreeView on DoubleClick, but at the same time use a command for this event, you could use this solution:

https://stackoverflow.com/a/60869105/1206431

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