Skip to content
This repository was archived by the owner on Nov 1, 2024. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Avalonia.Controls;
using System.Linq;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Xaml.Interactivity;
Expand Down Expand Up @@ -52,6 +53,7 @@ protected override void OnAttachedToVisualTree()
{
DragDrop.SetAllowDrop(AssociatedObject, true);
}

AssociatedObject?.AddHandler(DragDrop.DragEnterEvent, DragEnter);
AssociatedObject?.AddHandler(DragDrop.DragLeaveEvent, DragLeave);
AssociatedObject?.AddHandler(DragDrop.DragOverEvent, DragOver);
Expand All @@ -65,6 +67,7 @@ protected override void OnDetachedFromVisualTree()
{
DragDrop.SetAllowDrop(AssociatedObject, false);
}

AssociatedObject?.RemoveHandler(DragDrop.DragEnterEvent, DragEnter);
AssociatedObject?.RemoveHandler(DragDrop.DragLeaveEvent, DragLeave);
AssociatedObject?.RemoveHandler(DragDrop.DragOverEvent, DragOver);
Expand All @@ -73,6 +76,11 @@ protected override void OnDetachedFromVisualTree()

private void DragEnter(object? sender, DragEventArgs e)
{
if (!IsExpectedFormatAvailable(e))
{
return;
}

var sourceContext = e.Data.Get(ContextDropBehavior.DataFormat);
var targetContext = Context ?? AssociatedObject?.DataContext;
Handler?.Enter(sender, e, sourceContext, targetContext);
Expand All @@ -85,15 +93,39 @@ private void DragLeave(object? sender, RoutedEventArgs e)

private void DragOver(object? sender, DragEventArgs e)
{
if (!IsExpectedFormatAvailable(e))
{
return;
}

var sourceContext = e.Data.Get(ContextDropBehavior.DataFormat);
var targetContext = Context ?? AssociatedObject?.DataContext;
Handler?.Over(sender, e, sourceContext, targetContext);
}

private void Drop(object? sender, DragEventArgs e)
{
if (!IsExpectedFormatAvailable(e))
{
return;
}

var sourceContext = e.Data.Get(ContextDropBehavior.DataFormat);
var targetContext = Context ?? AssociatedObject?.DataContext;
Handler?.Drop(sender, e, sourceContext, targetContext);
}


private static bool IsExpectedFormatAvailable(DragEventArgs e)
{
var availableFormats = e.Data.GetDataFormats();
if (availableFormats.Contains(ContextDropBehavior.DataFormat))
{
return true;
}

e.Handled = true;
e.DragEffects = DragDropEffects.None;
return false;
}
}