private TreeViewItem FileTreeHitTest(MouseButtonEventArgs e) { var element = FileTree.InputHitTest(e.GetPosition(FileTree)) as DependencyObject; while (!(element is TreeViewItem) && element != null) { element = VisualTreeHelper.GetParent(element); } var item = (TreeViewItem)element; if (item != null) { item.IsSelected = true; } return(item); }