Reputation: 925
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?
Upvotes: 0
Views: 215
Reputation: 23945
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());
}
}
I'm pretty sure the SelectedItem is the object you are looking for.
Upvotes: 1