private void FolderTreeView_OnPreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e) { TreeViewItem treeViewItem = WPFHelper.VisualTVUpwardSearch(e.OriginalSource as DependencyObject); if (treeViewItem != null) { treeViewItem.Focus(); e.Handled = true; } if (treeViewItem != null) { // click on item var cms = new ContextMenu { Items = { new AutoActionMenuItem { Header = "Add subfolder", AlephAction = "AddSubFolder", ParentAnchor = ParentAnchor }, new AutoActionMenuItem { Header = "Rename folder", AlephAction = "RenameFolder", ParentAnchor = ParentAnchor }, new Separator(), new AutoActionMenuItem { Header = "Delete folder", AlephAction = "DeleteFolder", ParentAnchor = ParentAnchor }, } }; FolderTreeView.ContextMenu = null; WPFHelper.ExecDelayed(100, () => { FolderTreeView.ContextMenu = cms; cms.IsOpen = true; }); } else { // click on free space var cms = new ContextMenu { Items = { new AutoActionMenuItem { Header = "Add subfolder", AlephAction = "AddSubFolder", ParentAnchor = ParentAnchor }, } }; FolderTreeView.ContextMenu = cms; cms.IsOpen = true; } }