Fixed an issue where if you dragged a device to the screen, it used the last selected instead,

This commit is contained in:
Matěj Kubíček
2026-03-28 21:25:27 +01:00
parent e46f38d153
commit b77d2f12aa
+13 -2
View File
@@ -75,10 +75,21 @@ namespace NetworkDiagram
#region Drag and Drop (Toolbox to Canvas) #region Drag and Drop (Toolbox to Canvas)
private void Toolbox_PreviewMouseDown(object sender, MouseButtonEventArgs e) private void Toolbox_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{ {
if (sender is ListBox { SelectedItem: DeviceTemplate template } listBox) if (sender is ListBox listBox)
DragDrop.DoDragDrop(listBox, template, DragDropEffects.Copy); {
// 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) private void DiagramCanvas_DragOver(object sender, DragEventArgs e)
{ {
e.Effects = e.Data.GetDataPresent(typeof(DeviceTemplate)) ? DragDropEffects.Copy : DragDropEffects.None; e.Effects = e.Data.GetDataPresent(typeof(DeviceTemplate)) ? DragDropEffects.Copy : DragDropEffects.None;