I did not know UWP. But I know about Drag & Drop with Windows and think that this answers your questions.
You are missing that you must also handle the
DragOver
event and that the handlers must set the
DragEventArgs Class[
^]
Effect Property[
^] to indicate what is allowed.
The effect returned by
DragEnter
and
DragOver
indicates if dropping is possible and if so if data are copied or moved (usually by checking the
KeyState
property). This return value is used by the system to generate the drag cursor (e.g. stop sign if dropping not possible).
When the mouse button is released, the system calls once DragOver
to get the effect. If dropping is possible, the Drop
handler is called. Otherwise the DragLeave
handler is called (this must only be implemented when you need to perform some cleanup).
[EDIT: The above is wrong]
When the mouse button is released, the system calls once the
DragOver
handler to set the effect and then the
Drop
handler.
The optional
DragLeave
handler is called when leaving the window with the mouse button still down. It can be used for cleanup. Note that this is not called when the drop handler has been called and it may be necessary to add similar cleanup code to the drop handler.
[/EDIT]
The drag source can use the
DropCompleted
event to check what has happened. When data has been moved, it should delete it's own data.
[UPDATE]
Note again that I don't know UWP and these examples might not work. But they should show how to do it.
So you should add the
DragOver
handler and modify the others:
private void tbDrop_DragEnter(object sender, DragEventArgs e)
{
this.canDrop = CanDrop(sender, e);
DragOver(sender, e);
}
private void tbDrop_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.None;
if (this.canDrop && CanDropHere(sender, e))
{
if ((e.KeyState & 8) == 8 &&
(e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy)
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.Move;
}
}
}
private void tbDrop_Drop(object sender, DragEventArgs e)
{
if (e.Effect != DragDropEffects.None)
{
}
this.canDrop = false;
}
private void tbDrop_DragLeave(object sender, DragEventArgs e)
{
this.canDrop = false;
}
On the drag source side use the
DropCompleted
handler to detect if dropping has been occured and if data should be moved or copied.
private void tbDragSource_DropCompleted(UIElement elem, DropCompletedEventArgs e)
{
if (e.DropResult == DataPackageOperation.Move)
{
}
}
[/UPDATE]