Browse Source

Fix DnD page when dropping on textblocks

pull/20926/head
Julien Lebosquain 7 months ago
parent
commit
a15e1693bd
No known key found for this signature in database GPG Key ID: 1833CAD10ACC46FD
  1. 35
      samples/ControlCatalog/Pages/DragAndDropPage.xaml.cs

35
samples/ControlCatalog/Pages/DragAndDropPage.xaml.cs

@ -51,6 +51,9 @@ namespace ControlCatalog.Pages
"Bitmap", "Bitmap",
d => d.Add(DataTransferItem.Create(DataFormat.Bitmap, new Bitmap(AssetLoader.Open(new Uri("avares://ControlCatalog/Assets/image1.jpg"))))), d => d.Add(DataTransferItem.Create(DataFormat.Bitmap, new Bitmap(AssetLoader.Open(new Uri("avares://ControlCatalog/Assets/image1.jpg"))))),
DragDropEffects.Copy); DragDropEffects.Copy);
AddHandlers(CopyTarget, DragDropEffects.Copy);
AddHandlers(MoveTarget, DragDropEffects.Move);
} }
private void SetupDnd(string suffix, Action<DataTransfer> factory, DragDropEffects effects) => private void SetupDnd(string suffix, Action<DataTransfer> factory, DragDropEffects effects) =>
@ -94,16 +97,18 @@ namespace ControlCatalog.Pages
} }
} }
dragMe.PointerPressed += DoDrag;
}
private void AddHandlers(Control target, DragDropEffects allowedEffects)
{
DragDrop.AddDragEnterHandler(target, DragOver);
DragDrop.AddDragOverHandler(target, DragOver);
DragDrop.AddDropHandler(target, Drop);
void DragOver(object? sender, DragEventArgs e) void DragOver(object? sender, DragEventArgs e)
{ {
if (e.Source is Control c && c.Name == "MoveTarget") e.DragEffects &= allowedEffects;
{
e.DragEffects = e.DragEffects & (DragDropEffects.Move);
}
else
{
e.DragEffects = e.DragEffects & (DragDropEffects.Copy);
}
// Only allow if the dragged data contains text or filenames. // Only allow if the dragged data contains text or filenames.
if (!e.DataTransfer.Contains(DataFormat.Text) if (!e.DataTransfer.Contains(DataFormat.Text)
@ -115,14 +120,7 @@ namespace ControlCatalog.Pages
async void Drop(object? sender, DragEventArgs e) async void Drop(object? sender, DragEventArgs e)
{ {
if (e.Source is Control c && c.Name == "MoveTarget") e.DragEffects &= allowedEffects;
{
e.DragEffects = e.DragEffects & (DragDropEffects.Move);
}
else
{
e.DragEffects = e.DragEffects & (DragDropEffects.Copy);
}
if (e.DragEffects == DragDropEffects.None) if (e.DragEffects == DragDropEffects.None)
return; return;
@ -168,11 +166,6 @@ namespace ControlCatalog.Pages
DropState.Content = "Custom: " + e.DataTransfer.TryGetValue(_customFormat); DropState.Content = "Custom: " + e.DataTransfer.TryGetValue(_customFormat);
} }
} }
dragMe.PointerPressed += DoDrag;
AddHandler(DragDrop.DropEvent, Drop);
AddHandler(DragDrop.DragOverEvent, DragOver);
} }
} }
} }

Loading…
Cancel
Save