0
votes

In my treeview I have text. After I select that, I want to retrieve that selected item as string and I need to pass this string to various functions.

I don't know how to get the selected item.I coded like

private void treeview1_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            TreeViewItem selectedTVI = null;
            if (treeview1.SelectedItem != null)
            {
                selectedTVI = treeview1.Tag as TreeViewItem;
            }
        }

But selectedTVI shows NULL.What can I do?

1

1 Answers

1
votes

TreeViews display lists of items, not lists of TreeViewItems.

TreeViewItem.SelectedItem is the element that is selected, if your tree view has a collection of Car objects that it is displaying, the SelectedItem will be of type Car.

try this

private void treeview1_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            if (treeview1.SelectedItem != null)
            {
                Console.WriteLine(treeview1.SelectedItem.ToString());
            }
        }

im pretty sure the SelectedItem is the object you are looking for.

(by the way your 20% acceptance rate sucks a little - and is probably one of the reasons you dont get your questions answered quicker, if your question is answered, mark it as answered. This helps the whole community)