Wow, this is UGLY. The WPF tree view is my favorite target for this adjective in many ways, but I have not encountered this particular problem before.
I donโt think you can do better than Continue for KeyboardNavigation.TabNavigation , the only option is Local , and this also does not work. See this msdn article . In addition, combinations like Cycle for elements and Continue on the control did not give the desired result for me.
It is quite obvious that the selected item is saved in the tree view when the focus returns to the list, and the next time the tree receives focus, it goes directly to the previously selected item (tree item 2), unlike listview, which retains the selection, but correctly focuses the control first, and then the elements.
So a quick and dirty hack that might work for you is to remove the selection from the tree when it loses focus. Unfortunately, the event fires every time the selected item changes, but the hack still works.
<TreeView (...) LostFocus="TreeView_LostFocus">
code behind:
private void TreeView_LostFocus(object sender, RoutedEventArgs e) { TreeView tv = (TreeView)sender; TreeViewItem item = tv.SelectedItem as TreeViewItem; if (item != null) item.IsSelected = false; }
source share