只是为了回答我自己的问题,也许它对某人有用。
我没有发现问题出在哪里,但我的结论是,在某些 PC 上,当用户在拖动时按下鼠标中键/右键时,DragDrop 可能会中断。
要覆盖此行为,您需要将QueryContinueDragHandler添加到 DragDrop。然后在您自己的处理程序中使用您的逻辑来响应鼠标/键盘输入。
所以我的代码如下所示:
DragDrop.AddQueryContinueDragHandler(this, QueryContinueDragHandler);
DragDrop.DoDragDrop(this, dataObject, DragDropEffects.Copy);
DragDrop.RemoveQueryContinueDragHandler(this, QueryContinueDragHandler);
和自定义处理程序:
/// <summary>
/// Own handler. This event is raised when something happens during DragDrop operation (user presses Mouse button or Keyboard button...)
/// Necessary to avoid canceling DragDrop on MouseMiddleButon on certain PCs.
/// Overrides default handler, that interrupts DragDrop on MouseMiddleButon or MouseRightButton down.
/// </summary>
/// <param name="source"></param>
/// <param name="e"></param>
private void QueryContinueDragHandler(Object source, QueryContinueDragEventArgs e)
{
e.Handled = true;
// if ESC
if (e.EscapePressed)
{
// --> cancel DragDrop
e.Action = DragAction.Cancel;
return;
}
// if LB
if (e.KeyStates.HasFlag(DragDropKeyStates.LeftMouseButton))
{
// --> continue dragging
e.Action = DragAction.Continue;
}
// if !LB (user released LeftMouseButton)
else
{
// and if mouse is inside canvas
if (_isMouseOverCanvas)
{
// --> execute Drop
e.Action = DragAction.Drop;
}
else
{
// --> cancel Drop
e.Action = DragAction.Cancel;
}
return;
}
// if MB
if (e.KeyStates.HasFlag(DragDropKeyStates.MiddleMouseButton))
{
// --> continue dragging
e.Action = DragAction.Continue;
}
}