DragDrop.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System.Threading.Tasks;
  2. using Avalonia.Input.Platform;
  3. using Avalonia.Interactivity;
  4. namespace Avalonia.Input
  5. {
  6. public static class DragDrop
  7. {
  8. /// <summary>
  9. /// Event which is raised, when a drag-and-drop operation enters the element.
  10. /// </summary>
  11. public static readonly RoutedEvent<DragEventArgs> DragEnterEvent = RoutedEvent.Register<DragEventArgs>("DragEnter", RoutingStrategies.Bubble, typeof(DragDrop));
  12. /// <summary>
  13. /// Event which is raised, when a drag-and-drop operation leaves the element.
  14. /// </summary>
  15. public static readonly RoutedEvent<RoutedEventArgs> DragLeaveEvent = RoutedEvent.Register<RoutedEventArgs>("DragLeave", RoutingStrategies.Bubble, typeof(DragDrop));
  16. /// <summary>
  17. /// Event which is raised, when a drag-and-drop operation is updated while over the element.
  18. /// </summary>
  19. public static readonly RoutedEvent<DragEventArgs> DragOverEvent = RoutedEvent.Register<DragEventArgs>("DragOver", RoutingStrategies.Bubble, typeof(DragDrop));
  20. /// <summary>
  21. /// Event which is raised, when a drag-and-drop operation should complete over the element.
  22. /// </summary>
  23. public static readonly RoutedEvent<DragEventArgs> DropEvent = RoutedEvent.Register<DragEventArgs>("Drop", RoutingStrategies.Bubble, typeof(DragDrop));
  24. public static readonly AttachedProperty<bool> AllowDropProperty = AvaloniaProperty.RegisterAttached<Interactive, bool>("AllowDrop", typeof(DragDrop), inherits: true);
  25. /// <summary>
  26. /// Gets a value indicating whether the given element can be used as the target of a drag-and-drop operation.
  27. /// </summary>
  28. public static bool GetAllowDrop(Interactive interactive)
  29. {
  30. return interactive.GetValue(AllowDropProperty);
  31. }
  32. /// <summary>
  33. /// Sets a value indicating whether the given interactive can be used as the target of a drag-and-drop operation.
  34. /// </summary>
  35. public static void SetAllowDrop(Interactive interactive, bool value)
  36. {
  37. interactive.SetValue(AllowDropProperty, value);
  38. }
  39. /// <summary>
  40. /// Starts a dragging operation with the given <see cref="IDataObject"/> and returns the applied drop effect from the target.
  41. /// <seealso cref="DataObject"/>
  42. /// </summary>
  43. public static Task<DragDropEffects> DoDragDrop(PointerEventArgs triggerEvent, IDataObject data, DragDropEffects allowedEffects)
  44. {
  45. var src = AvaloniaLocator.Current.GetService<IPlatformDragSource>();
  46. return src?.DoDragDrop(triggerEvent, data, allowedEffects) ?? Task.FromResult(DragDropEffects.None);
  47. }
  48. }
  49. }