在WPF的TreeView上的鼠标右键上获取treeviewItem [英] Getting the treeviewItem on the RightClick of Mouse On WPF treeView

查看:839
本文介绍了在WPF的TreeView上的鼠标右键上获取treeviewItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




在鼠标的RightClick事件上获取WPF TreeviewItem
指导我 我在 TreeView1_SelectedItemChanged 事件上得到TreeViewItem,但在鼠标右键单击上找不到treeViewItm.

谢谢和问候
vilas

Hi ,


Please Guide me for the getting the WPF TreeviewItem on the RightClick event of Mouse
I get the TreeViewItem on TreeView1_SelectedItemChanged event but Not able to find the treeViewItm on Right click of mouse.

Thanks and regards
vilas

推荐答案

在TreeView上单击鼠标右键不会选择节点,因此没有"node"可供选择.您可以通过将"sender"参数强制转换为TreeView对象,然后使用SelectedItem属性(返回选定的节点对象)或SelectedItemPath来获取事件处理程序中的当前选定的节点.遍历控件层次结构并获取选定的节点.但是我猜那不是你想要的.

不幸的是,TV控件不能轻易返回用户右键单击的节点……发送者是TreeView,并且我相信e.OriginalSource属性也返回TreeView(来自此处的内存...)使用了一种依赖于System.Windows.Input中的Mouse.DirectlyOver函数的解决方法,以返回鼠标悬停的UIElement.假设用户右键单击时鼠标悬停在TreeViewItem上,则应该能够在Try..Catch块中将返回的UI元素类型转换为TreeViewItem.如果成功,您将知道用户在TVI元素上,现在您可以对其进行引用.如果您在进行类型转换后最终陷入困境,那么您右键单击鼠标时会知道鼠标悬停在其他物体上.当然,如果树形视图上还有其他UI元素(例如面板或忙碌指示器),则必须将它们设置为折叠状态,而不仅仅是透明状态.否则,您会得到它们,因为它们在Z轴顺序中较高.

HTH
Right clicking the mouse on a TreeView does not select a node therefore there is no "node" to select. You can get the currently selected node within the event handler by casting the ''sender'' parameter to a TreeView object then using the SelectedItem property (which returns the node object that is selected) or the SelectedItemPath to walk the control hierarchy and get the selected node. But I''m guessing that isn''t what you want.

Unfortunately, the TV control doesn''t readily return the node that a user right-clicks on... the sender is the TreeView and I believe the e.OriginalSource property also returns the TreeView (going from memory here...) I used a workaround that relied on the Mouse.DirectlyOver function in System.Windows.Input to return the UIElement the mouse is over. Assuming the mouse is over a TreeViewItem when the user right-clicked, you should be able to type-cast the returned UI element to a TreeViewItem in a Try..Catch block. If successful, you know the user was over a TVI element and now you have a reference to it. If you end up in the catch after the type-cast, you know the mouse was over something else when right-clicked. Of course, if you have other UI elements over the treeview, like panels or busy indicators, they must be set to collapsed rather than just transparent. Otherwise you will get them because they are higher in the Z-Order.

HTH


这篇关于在WPF的TreeView上的鼠标右键上获取treeviewItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆