private void CopyItem(NpcTreeViewItem sourceItem, NpcTreeViewItem targetItem) { //Asking user wether he want to drop the dragged TreeViewItem here or not if (MessageBox.Show("Would you like to drop " + sourceItem.Header + " into " + targetItem.Header + "", "", MessageBoxButton.YesNo) != MessageBoxResult.Yes) { return; } try { //adding dragged TreeViewItem in target TreeViewItem AddChild(sourceItem, targetItem); //finding Parent TreeViewItem of dragged TreeViewItem var parentItem = FindVisualParent <NpcTreeViewItem>(sourceItem); // if parent is null then remove from TreeView else remove from Parent TreeViewItem if (parentItem == null) { Items.Remove(sourceItem); } else { parentItem.Items.Remove(sourceItem); } } catch { // ignored } }
private NpcTreeViewItem GetItem(DirectoryInfo directory) { var item = new NpcTreeViewItem(directory, filter); item.AddDummy(); item.Expanded += ItemExpanded; return(item); }
private static void AddChild(NpcTreeViewItem sourceItem, NpcTreeViewItem targetItem) { if (sourceItem == null) { throw new ArgumentNullException(nameof(sourceItem)); } // add item in target TreeViewItem if (sourceItem.Tag is FileInfo sourceFile) { sourceFile.MoveTo(Path.Combine(((DirectoryInfo)targetItem.Tag).FullName, sourceFile.Name)); if (!targetItem.HasDummy) { targetItem.Items.Add(new NpcTreeViewItem(sourceFile)); } } }
private void StartDrag(MouseEventArgs e) { draggedItem = (NpcTreeViewItem)SelectedItem; if (draggedItem != null && !draggedItem.IsDirectoryNode) { var finalDropEffect = DragDrop.DoDragDrop(this, SelectedValue, DragDropEffects.Move); //Checking target is not null and item is if (finalDropEffect == DragDropEffects.Move && target != null) { // A Move drop was accepted if (!draggedItem.Header.ToString().Equals(target.Header.ToString())) { CopyItem(draggedItem, target); target = null; draggedItem = null; } } } }
protected void OnItemDoubleClicked(NpcTreeViewItem item) { ItemDoubleClicked?.Invoke(this, item); }
private static bool CheckDropTarget(NpcTreeViewItem sourceItem, NpcTreeViewItem targetItem) { //Check whether the target item is meeting your condition return(targetItem.IsDirectoryNode && !sourceItem.IsDirectoryNode); }