3

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?

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
Eric after dark
  • 1,768
  • 4
  • 31
  • 79

4 Answers4

4

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.

Community
  • 1
  • 1
Eric after dark
  • 1,768
  • 4
  • 31
  • 79
2

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

MarcAndre
  • 21
  • 1
0

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>
Vimal CK
  • 3,543
  • 1
  • 26
  • 47
  • I'm getting an `InvalidCastException` run time error for the `TreeView itm = (TreeView)...` line. And then when I try to fix that by entering in `TreeViewItem` it says that `System...TreeViewItem` does not contain a definition for 'SelectedItem'`... Why is this? I feel like this shouldn't be happening. – Eric after dark Aug 20 '13 at 18:12
  • @Eric after dark : Not "TreeViewItem itm", it is "Item itm". You are casting selectedItem to TreviewItem. that is wrong. Please see my code and use the same line – Vimal CK Aug 20 '13 at 19:09
  • I am not using a `List` though, I am using an `ObservableCollection`. – Eric after dark Aug 20 '13 at 19:22
-2

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;
Robin B
  • 1,066
  • 1
  • 14
  • 32