DragDropPage.axaml.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System;
  2. using Avalonia;
  3. using Avalonia.Controls;
  4. using Avalonia.Input;
  5. using Avalonia.Input.Platform;
  6. using Avalonia.Interactivity;
  7. namespace IntegrationTestApp.Pages;
  8. public partial class DragDropPage : UserControl
  9. {
  10. public DragDropPage()
  11. {
  12. InitializeComponent();
  13. // Set up drag-drop event handlers
  14. AddHandler(DragDrop.DragOverEvent, DropTarget_DragOver);
  15. AddHandler(DragDrop.DropEvent, DropTarget_Drop);
  16. }
  17. private async void DragSource_PointerPressed(object? sender, PointerPressedEventArgs e)
  18. {
  19. if (e.GetCurrentPoint(this).Properties.IsLeftButtonPressed)
  20. {
  21. var dragData = new DataTransfer();
  22. dragData.Add(DataTransferItem.CreateText("TestDragData"));
  23. DragDropStatus.Text = "Dragging...";
  24. var result = await DragDrop.DoDragDropAsync(e, dragData, DragDropEffects.Copy | DragDropEffects.Move);
  25. DragDropStatus.Text = result switch
  26. {
  27. DragDropEffects.Copy => "Copied",
  28. DragDropEffects.Move => "Moved",
  29. DragDropEffects.None => "Cancelled",
  30. _ => $"Result: {result}"
  31. };
  32. }
  33. }
  34. private void DropTarget_DragOver(object? sender, DragEventArgs e)
  35. {
  36. // Only handle events for the drop target
  37. if (e.Source != DropTarget && !IsChildOf(e.Source as Visual, DropTarget))
  38. return;
  39. e.DragEffects = DragDropEffects.Copy;
  40. // Get the position relative to the drop target
  41. var position = e.GetPosition(DropTarget);
  42. DropPosition.Text = $"DragOver: ({position.X:F0}, {position.Y:F0})";
  43. }
  44. private void DropTarget_Drop(object? sender, DragEventArgs e)
  45. {
  46. // Only handle events for the drop target
  47. if (e.Source != DropTarget && !IsChildOf(e.Source as Visual, DropTarget))
  48. return;
  49. // Get the position relative to the drop target
  50. var position = e.GetPosition(DropTarget);
  51. DropPosition.Text = $"Drop: ({position.X:F0}, {position.Y:F0})";
  52. // Check if the position is within reasonable bounds of the drop target
  53. var bounds = DropTarget.Bounds;
  54. var isWithinBounds = position.X >= 0 && position.X <= bounds.Width &&
  55. position.Y >= 0 && position.Y <= bounds.Height;
  56. var text = e.DataTransfer.TryGetText();
  57. if (text != null)
  58. {
  59. DropTargetText.Text = isWithinBounds
  60. ? $"Dropped: {text} at ({position.X:F0}, {position.Y:F0})"
  61. : $"ERROR: Position out of bounds! ({position.X:F0}, {position.Y:F0})";
  62. DragDropStatus.Text = isWithinBounds ? "Drop OK" : "Drop position ERROR";
  63. }
  64. e.DragEffects = DragDropEffects.Copy;
  65. }
  66. private static bool IsChildOf(Visual? child, Visual? parent)
  67. {
  68. if (child == null || parent == null)
  69. return false;
  70. var current = child.Parent as Visual;
  71. while (current != null)
  72. {
  73. if (current == parent)
  74. return true;
  75. current = current.Parent as Visual;
  76. }
  77. return false;
  78. }
  79. private void ResetDragDrop_Click(object? sender, RoutedEventArgs e)
  80. {
  81. DropPosition.Text = string.Empty;
  82. DragDropStatus.Text = string.Empty;
  83. DropTargetText.Text = "Drop items here";
  84. }
  85. }