捕获 TreeView 项目上的单击

发布于 2024-11-17 06:06:49 字数 252 浏览 3 评论 0原文

有没有办法显式捕获对 TreeView 项目的点击?

目前,我的处境是电视代表了用户的一种选项菜单。通常,我只会在电视上使用 SelectedItemChanged 事件,但用户有可能连续两次重新单击某个项目,而不会引发此事件。我考虑过在包含每个节点中的项目的堆栈面板上放置一个事件侦听器的想法,但问题是该事件在节点上的选定项目更改之前触发。因此,如果您点击堆栈面板,您将获得该事件,但无法获得关联的项目。关于如何摆脱这个困境有什么想法吗?

Is there a way to explicitly capture a click on a TreeView item?

Currently, I'm in a position where a TV represents a sort of options menu for users. Ordinarily, I would just use the SelectedItemChanged event on the TV, but there is the possibility of a user re-clicking an item twice in a row which won't raise this event. I considered the idea of putting an event listener on the stackpanel that contains the items in each node, but the problem is this event fires before a selected item changed on a node. So if you hit the stackpanel, you'll get the event, but won't be able to get the associated item. Any ideas on how to get out of this one?

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

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

发布评论

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

评论(1

往事风中埋 2024-11-24 06:06:49

您可以处理 TreeViewMouseLeftButtonUp 事件。

private void treeView1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    var selectedItem = treeView1.SelectedItem;
}

[编辑]

您可以处理MouseLeftButtonDown,但此时SelectedItem还不会改变。您可以做的是访问e.OriginalSource以找出单击了哪个元素。我使用 树检查了下面的示例,因此元素是 TextBlock。如果您尝试下面的代码,请根据您的场景更改类型转换。

public MainPage()
{
    InitializeComponent();
    treeView1.AddHandler(MouseLeftButtonDownEvent, new MouseButtonEventHandler(treeView1_MouseLeftButtonDown), true);
}

private void treeView1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    var selectedElement = (TextBlock)e.OriginalSource;
}

You could handle MouseLeftButtonUp event for the TreeView.

private void treeView1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    var selectedItem = treeView1.SelectedItem;
}

[EDIT]

You can handle MouseLeftButtonDown, but SelectedItem will not be changed yet at that moment. What you can do is to access e.OriginalSource to find out what element was clicked. I checked the example below with a tree of <TreeViewItem />, so element was TextBlock. If you try the code below change the type casting according to your scenario.

public MainPage()
{
    InitializeComponent();
    treeView1.AddHandler(MouseLeftButtonDownEvent, new MouseButtonEventHandler(treeView1_MouseLeftButtonDown), true);
}

private void treeView1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    var selectedElement = (TextBlock)e.OriginalSource;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文