我希望能够通过右键单击在我的程序中选择一个 TreeViewItem
。以前,(在 this question 中)我试图通过从任何我想允许选择 TreeViewItem
的地方调用 SetSelectedItem()
方法来做到这一点。该问题的答案已编译并运行,但实际上并没有让 TreeViewItem
像我想要的那样被选中。
This question除了 hierachicalDataTemplate
之外,我一直在研究的问题与这个问题几乎完全相同。我的 TreeView
没有 hierachicalDataTemplate
,如果我的程序不需要它,我想避免使用它。
这是我正在编译的内容,但现在不影响更改...
//Sets selected item in TreeView and passes to MainWindowViewModel
private void SetSelectedItem()
{
MainWindowViewModel.SelectedItem = Tree_One.SelectedItem as TreeViewItem;
}
//**** This is the function this question is about -- It's Supposed to select item on RightClick
private void Tree_One_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
SetSelectedItem();
}
所以为了清楚起见,我右键单击的节点没有按预期被选中。我做错了什么,我该如何解决?
更新:
我想我在试过下面的答案后就知道问题出在哪里了。我在这个问题中的代码实际上并没有改变选定的项目,它只是通过选择当前选定的项目来重申,重新选择它。如果有一种方法可以将所选项目实际更改为右键单击的项目,它将完美运行。关于如何做这样的事情的任何线索?
最佳答案
@alex2k8 在 this question 上的回答正是我一直在寻找的东西,也是我用来解决问题的东西。
感谢所有提供帮助的人。
关于c# - 右键单击选择一个 TreeViewItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18339507/