From b77d2f12aa77ef0a0500ad2cb9fdefb0c4f1145e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C4=9Bj=20Kub=C3=AD=C4=8Dek?= Date: Sat, 28 Mar 2026 21:25:27 +0100 Subject: [PATCH] Fixed an issue where if you dragged a device to the screen, it used the last selected instead, --- NetworkDiagram/MainWindow.xaml.cs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/NetworkDiagram/MainWindow.xaml.cs b/NetworkDiagram/MainWindow.xaml.cs index 3f34dbe..63272ec 100644 --- a/NetworkDiagram/MainWindow.xaml.cs +++ b/NetworkDiagram/MainWindow.xaml.cs @@ -75,10 +75,21 @@ namespace NetworkDiagram #region Drag and Drop (Toolbox to Canvas) private void Toolbox_PreviewMouseDown(object sender, MouseButtonEventArgs e) { - if (sender is ListBox { SelectedItem: DeviceTemplate template } listBox) - DragDrop.DoDragDrop(listBox, template, DragDropEffects.Copy); + if (sender is ListBox listBox) + { + // Find the item under the mouse instead of relying on SelectedItem + var element = e.OriginalSource as DependencyObject; + while (element != null && !(element is ListBoxItem)) + element = VisualTreeHelper.GetParent(element); + + if (element is ListBoxItem item && item.Content is DeviceTemplate template) + { + DragDrop.DoDragDrop(listBox, template, DragDropEffects.Copy); + } + } } + private void DiagramCanvas_DragOver(object sender, DragEventArgs e) { e.Effects = e.Data.GetDataPresent(typeof(DeviceTemplate)) ? DragDropEffects.Copy : DragDropEffects.None;