tags:

views:

54

answers:

1

Hi, In y treeview i have text,after i seelcted that,i want to retrive that selected item as string and i need to pass this string to various fucntions.

I dont 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?

A: 

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)

Aran Mulholland
How can i mark as answered?.Whether i have to clcik tick mark beside every answer.
Anu
yes you tick next to the answer that is correct (then the tick goes green). if there is no correct answer dont tick it.
Aran Mulholland