如何让 MouseLeftButtonDown 在 TreeViewItem 上工作,就像在 TextBlock 上工作一样?

发布于 2024-08-07 13:17:02 字数 1810 浏览 8 评论 0原文

如何将点击事件附加到 TreeViewItem?

以下内容适用于 TextBlock,但不适用于 TreeViewItem:

XAML:

<Window x:Class="TestClickTree2343.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <Style TargetType="TreeViewItem">
            <Setter Property="IsExpanded" Value="True" />
        </Style>
    </Window.Resources>

    <StackPanel>
        <TextBlock Text="Click this" MouseLeftButtonDown="TextBlock_MouseLeftButtonDown"/>
        <TreeViewItem Header="Files">
            <TreeViewItem Header="File 1">
                <TreeViewItem Header="Part 1">
                    <TreeViewItem Header="Paragraph 1" MouseLeftButtonDown="TreeViewItem_MouseLeftButtonDown"/>
                    <TreeViewItem Header="Paragraph 2"/>
                </TreeViewItem>
            </TreeViewItem>
        </TreeViewItem>
    </StackPanel>
</Window>

代码隐藏:

using System.Windows;
using System.Windows.Input;

namespace TestClickTree2343
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void TreeViewItem_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            MessageBox.Show("treeview item was clicked, this does NOT work");
        }

        private void TextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            MessageBox.Show("textblock item was clicked, this WORKS");
        }
    }
}

How can I attach a click event on to a TreeViewItem?

The following works for the TextBlock but not the TreeViewItem:

XAML:

<Window x:Class="TestClickTree2343.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <Style TargetType="TreeViewItem">
            <Setter Property="IsExpanded" Value="True" />
        </Style>
    </Window.Resources>

    <StackPanel>
        <TextBlock Text="Click this" MouseLeftButtonDown="TextBlock_MouseLeftButtonDown"/>
        <TreeViewItem Header="Files">
            <TreeViewItem Header="File 1">
                <TreeViewItem Header="Part 1">
                    <TreeViewItem Header="Paragraph 1" MouseLeftButtonDown="TreeViewItem_MouseLeftButtonDown"/>
                    <TreeViewItem Header="Paragraph 2"/>
                </TreeViewItem>
            </TreeViewItem>
        </TreeViewItem>
    </StackPanel>
</Window>

Code-Behind:

using System.Windows;
using System.Windows.Input;

namespace TestClickTree2343
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void TreeViewItem_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            MessageBox.Show("treeview item was clicked, this does NOT work");
        }

        private void TextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            MessageBox.Show("textblock item was clicked, this WORKS");
        }
    }
}

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

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

发布评论

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

评论(2

ま柒月 2024-08-14 13:17:02

尝试使用 PreviewMouseLeftButtonDown 事件而不是 MouseLeftButtonDown。

根据 MSDN 文档,PreviewMouseLeftButtonDownMouseLeftButtonDown 使用直接路由策略,以便我不太清楚为什么会出现这种情况。但是,文档可能不正确,因为通常“预览”事件使用隧道策略,而其对应事件则使用气泡。

Trying using the PreviewMouseLeftButtonDown event rather than MouseLeftButtonDown.

Accoding to the MSDN docs both the PreviewMouseLeftButtonDown and MouseLeftButtonDown using a Direct routing strategy so I am not too sure why this is the case. However, it is possible that the documentation is incorrect, as generally 'Preview' events use the Tunneling strategy while their counterparts using the Bubble.

浅暮の光 2024-08-14 13:17:02

上述解决方案有效,但在父节点上单击时它会阻止展开树视图节点。

问候

The above solution works but it prevents expanding the treeview nodes when cliked on parent node.

Regards

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