0

I have the same problem as a previous question on this forum:

previous thread

I have a TreeView which uses a HierarchicalDataTemplate to bind its data. I need to get the TreeViewItem from the selected item, which is my own class.

I have tried the solution given in the previous thread as well as modified versions of it. Nothing works. myTreeView.Items.CurrentPosition returns -1. If I check inside Items I can only see my 2 root items. I have several levels of items.

myTreeView.ItemContainerGenerator.ContainerFromObject(myTreeView.SelectedItem) doesn't work either, it returns null. myTreeView.ItemContainerGenerator.ContainerFromObject(myTreeView.Items.CurrentObject(myTreeView.Items.CurrentItem) returns null.

Community
  • 1
  • 1

1 Answers1

0

this is what you need:

private void TreeView_OnSelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
    object entity = null;
    entity = e.NewValue;
}
Natrium
  • 30,772
  • 17
  • 59
  • 73