All of the previous answers will be helpful when explicitly creating a TreeView using TreeViewItem (s). If you need a solution to clear selection when using ItemsSource, use the following code:
private static TreeViewItem FindTreeViewSelectedItemContainer(ItemsControl root, object selection) { var item = root.ItemContainerGenerator.ContainerFromItem(selection) as TreeViewItem; if (item == null) { foreach (var subItem in root.Items) { item = FindTreeViewSelectedItemContainer((TreeViewItem)root.ItemContainerGenerator.ContainerFromItem(subItem), selection); if (item != null) { break; } } } return item; }
source share