wpf 右键单击选择一个 TreeViewItem
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18339507/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Select a TreeViewItem on Right-Click
提问by Eric after dark
I would like to be able to select a TreeViewItemin my program on right-click. Previously, (In this question) I tried to do this by calling to SetSelectedItem()method from wherever I wanted to allow a TreeViewItemto be selected. The answer from that question compiled and ran, but did not actually allow the TreeViewItemto become selected like I wanted.
我希望能够TreeViewItem通过右键单击在我的程序中选择一个。以前,(在这个问题中)我试图通过SetSelectedItem()从任何我想要允许TreeViewItem选择a 的地方调用 to方法来做到这一点。该问题的答案编译并运行,但实际上并没有TreeViewItem像我想要的那样被选中。
This questionthat I've been looking at is pretty much the exact same question as this one, with the exception of the hierachicalDataTemplate. My TreeViewdoes not have a hierachicalDataTemplate, and if it is unnecessary for my program I would like to avoid it.
这个问题,我一直在看几乎是完全一样的问题,因为这一个,用的除外hierachicalDataTemplate。我的TreeView没有hierachicalDataTemplate,如果我的程序不需要它,我想避免它。
This is what I have compiling, but not affecting change right now...
这是我编译的内容,但现在不影响更改...
//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();
}
So just for clarity, the node that I right click does not get selected like expected. What am I doing wrong and how can I fix it?
所以为了清楚起见,我右键单击的节点没有像预期的那样被选中。我做错了什么,我该如何解决?
UPDATE:
更新:
I think I know what the problem is after playing around with the answer below. The code I have in this question doesn't actually change the selected item, it just kind of reiterates through the selection of the currently selected item, re-selecting it. If there was a way to actually change the selected item to the item that is right clicked, it would run perfectly. Any clue on how to do something like that?
我想我知道在玩弄下面的答案后问题是什么。我在这个问题中的代码实际上并没有改变选定的项目,它只是通过选择当前选定的项目,重新选择它来重申。如果有一种方法可以将所选项目实际更改为右键单击的项目,它将完美运行。关于如何做这样的事情的任何线索?
Thanks for your help.
谢谢你的帮助。
回答by Eric after dark
The answer by @alex2k8 on this questionis exactly what I was looking for, and is what I used to solve my problem.
@alex2k8 对这个问题的回答正是我一直在寻找的,也是我用来解决我的问题的答案。
Thanks to anyone who helped out.
感谢任何提供帮助的人。
回答by MarcAndre
sorry for my bad englisch. Well I'm working with the MS VS 2017 Version 15.9.1.
抱歉我的英语不好。好吧,我正在使用 MS VS 2017 版本 15.9.1。
So - all your ways for selected a treeviewItem via right mouse click dosn't work - I don't know why.
所以 - 通过鼠标右键单击来选择一个 treeviewItem 的所有方法都不起作用 - 我不知道为什么。
But I find a working way:
但我找到了一种工作方式:
private void Treeview1_MouseRightButtonDown(object sender, MouseButtonEventArgs e){
// The source from the Mouse Event Args is a TreeViewItem.
var treeViewitem = e.Source as TreeViewItem;
// Than works your Code in the above Posts!
if (treeViewitem != null)
{
treeViewitem.IsSelected = true;
e.Handled = true;
}
}
cu Marc
古马克
回答by Vimal CK
Please see the sample snippet below wich is able to get the selected item
请参阅下面的示例片段,以便能够获取所选项目
public partial class MainWindow : Window
{
public List<Item> Items { get; set; }
public MainWindow()
{
InitializeComponent();
Items = new List<Item>();
for (int i = 0; i < 10; i++)
{
Items.Add(new Item() {ItemName="Item " + i.ToString() });
}
this.DataContext = this;
}
private void TreeView1_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
if ((sender as TreeView).SelectedItem != null)
{
Item itm = (Item)(sender as TreeView).SelectedItem;
Console.WriteLine(itm.ItemName);
}
}
}
public class Item
{
public string ItemName { get; set; }
}
XAML
XAML
<TreeView Name="TreeView1" MouseRightButtonDown="TreeView1_MouseRightButtonDown" ItemsSource="{Binding Items}">
<TreeView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding ItemName}" />
</DataTemplate>
</TreeView.ItemTemplate>
</TreeView>
回答by Robin B
This might be a bit outdated but I just found a very nice solution to this. At least imo.
这可能有点过时,但我刚刚找到了一个非常好的解决方案。至少海事组织。
TreeView now supports a NodeMouseClick event in which you can select the clicked node.
TreeView 现在支持 NodeMouseClick 事件,您可以在其中选择单击的节点。
private void treeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
this.treeView.SelectedNode = e.Node;

