捕获 TreeView 项目上的单击
有没有办法显式捕获对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以处理
TreeView
的MouseLeftButtonUp
事件。[编辑]
您可以处理
MouseLeftButtonDown
,但此时SelectedItem
还不会改变。您可以做的是访问e.OriginalSource
以找出单击了哪个元素。我使用
树检查了下面的示例,因此元素是TextBlock
。如果您尝试下面的代码,请根据您的场景更改类型转换。You could handle
MouseLeftButtonUp
event for theTreeView
.[EDIT]
You can handle
MouseLeftButtonDown
, butSelectedItem
will not be changed yet at that moment. What you can do is to accesse.OriginalSource
to find out what element was clicked. I checked the example below with a tree of<TreeViewItem />
, so element wasTextBlock
. If you try the code below change the type casting according to your scenario.