如何在 WPF 中为 TreeView 模拟右键单击的左键单击行为?

问题描述:

当我左键单击 TreeView 时,它会自动选择鼠标下的项目.我如何为右键单击执行此操作,以便右键单击时,它还选择鼠标下的项目?

When I left click on a TreeView, it automatically selects the item under mouse. How can I do this for right click so when you right click, it also selects the item under mouse?

我想这样做是因为当我右键单击时,我会根据 e.NewValue 在 SelectedItem 更改时设置 TreeView 的上下文菜单TreeView_SelectedItemChanged.但是右键单击本身不会更改 SelectedItem,这就是显示错误菜单的原因.或者我必须先左键单击我要选择的项目,然后右键单击.

I want to do this because when I right click, I set the context menu of the TreeView when the SelectedItem changes, based on the e.NewValue of TreeView_SelectedItemChanged. But the right click itself doesn't change the SelectedItem, that's why the wrong menu shows up. Or I have to first left click to the item I want selected, and then right click.

关于如何做到这一点的任何想法?

Any ideas on how to do this?

覆盖右键单击事件.这是一个例子.