Question

I would like to be able to select a TreeViewItem in 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 TreeViewItem to be selected. The answer from that question compiled and ran, but did not actually allow the TreeViewItem to become selected like I wanted.

This question that I've been looking at is pretty much the exact same question as this one, with the exception of the hierachicalDataTemplate. My TreeView does not have a hierachicalDataTemplate, and if it is unnecessary for my program I would like to avoid it.

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.

Was it helpful?

Solution

The answer by @alex2k8 on this question is exactly what I was looking for, and is what I used to solve my problem.

Thanks to anyone who helped out.

OTHER TIPS

sorry for my bad englisch. Well I'm working with the MS VS 2017 Version 15.9.1.

So - all your ways for selected a treeviewItem via right mouse click dosn't work - I don't know why.

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

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

<TreeView Name="TreeView1" MouseRightButtonDown="TreeView1_MouseRightButtonDown" ItemsSource="{Binding Items}">
        <TreeView.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding ItemName}" />
            </DataTemplate>
        </TreeView.ItemTemplate>
</TreeView>

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.

private void treeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
    if (e.Button == MouseButtons.Right)
    {
        this.treeView.SelectedNode = e.Node;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top