ContextDropBehavior.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using Avalonia.Controls;
  2. using Avalonia.Input;
  3. using Avalonia.Interactivity;
  4. using Avalonia.Xaml.Interactivity;
  5. namespace Avalonia.Xaml.Interactions.DragAndDrop;
  6. /// <summary>
  7. ///
  8. /// </summary>
  9. public class ContextDropBehavior : StyledElementBehavior<Control>
  10. {
  11. /// <summary>
  12. ///
  13. /// </summary>
  14. public static string DataFormat = nameof(Context);
  15. /// <summary>
  16. ///
  17. /// </summary>
  18. public static readonly StyledProperty<object?> ContextProperty =
  19. AvaloniaProperty.Register<ContextDropBehavior, object?>(nameof(Context));
  20. /// <summary>
  21. ///
  22. /// </summary>
  23. public static readonly StyledProperty<IDropHandler?> HandlerProperty =
  24. AvaloniaProperty.Register<ContextDropBehavior, IDropHandler?>(nameof(Handler));
  25. /// <summary>
  26. ///
  27. /// </summary>
  28. public object? Context
  29. {
  30. get => GetValue(ContextProperty);
  31. set => SetValue(ContextProperty, value);
  32. }
  33. /// <summary>
  34. ///
  35. /// </summary>
  36. public IDropHandler? Handler
  37. {
  38. get => GetValue(HandlerProperty);
  39. set => SetValue(HandlerProperty, value);
  40. }
  41. /// <inheritdoc />
  42. protected override void OnAttachedToVisualTree()
  43. {
  44. if (AssociatedObject is not null)
  45. {
  46. DragDrop.SetAllowDrop(AssociatedObject, true);
  47. }
  48. AssociatedObject?.AddHandler(DragDrop.DragEnterEvent, DragEnter);
  49. AssociatedObject?.AddHandler(DragDrop.DragLeaveEvent, DragLeave);
  50. AssociatedObject?.AddHandler(DragDrop.DragOverEvent, DragOver);
  51. AssociatedObject?.AddHandler(DragDrop.DropEvent, Drop);
  52. }
  53. /// <inheritdoc />
  54. protected override void OnDetachedFromVisualTree()
  55. {
  56. if (AssociatedObject is not null)
  57. {
  58. DragDrop.SetAllowDrop(AssociatedObject, false);
  59. }
  60. AssociatedObject?.RemoveHandler(DragDrop.DragEnterEvent, DragEnter);
  61. AssociatedObject?.RemoveHandler(DragDrop.DragLeaveEvent, DragLeave);
  62. AssociatedObject?.RemoveHandler(DragDrop.DragOverEvent, DragOver);
  63. AssociatedObject?.RemoveHandler(DragDrop.DropEvent, Drop);
  64. }
  65. private void DragEnter(object? sender, DragEventArgs e)
  66. {
  67. var sourceContext = e.Data.Get(ContextDropBehavior.DataFormat);
  68. var targetContext = Context ?? AssociatedObject?.DataContext;
  69. Handler?.Enter(sender, e, sourceContext, targetContext);
  70. }
  71. private void DragLeave(object? sender, RoutedEventArgs e)
  72. {
  73. Handler?.Leave(sender, e);
  74. }
  75. private void DragOver(object? sender, DragEventArgs e)
  76. {
  77. var sourceContext = e.Data.Get(ContextDropBehavior.DataFormat);
  78. var targetContext = Context ?? AssociatedObject?.DataContext;
  79. Handler?.Over(sender, e, sourceContext, targetContext);
  80. }
  81. private void Drop(object? sender, DragEventArgs e)
  82. {
  83. var sourceContext = e.Data.Get(ContextDropBehavior.DataFormat);
  84. var targetContext = Context ?? AssociatedObject?.DataContext;
  85. Handler?.Drop(sender, e, sourceContext, targetContext);
  86. }
  87. }