如何让 MouseLeftButtonDown 在 TreeViewItem 上工作,就像在 TextBlock 上工作一样?
如何将点击事件附加到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试使用 PreviewMouseLeftButtonDown 事件而不是 MouseLeftButtonDown。
根据 MSDN 文档,PreviewMouseLeftButtonDown和 MouseLeftButtonDown 使用直接路由策略,以便我不太清楚为什么会出现这种情况。但是,文档可能不正确,因为通常“预览”事件使用隧道策略,而其对应事件则使用气泡。
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.
上述解决方案有效,但在父节点上单击时它会阻止展开树视图节点。
问候
The above solution works but it prevents expanding the treeview nodes when cliked on parent node.
Regards