WPF:单击右/鼠标中键时DoDragDrop被中断

时间:2012-10-16 13:27:13

标签: c# wpf drag-and-drop devexpress mousemove

我已经扩展了Canvas {System.Windows.Controls}以及可拖动到Canvas中的项目。 在拖动过程中,我有OnDragOver事件,我在用户点击并按住鼠标中键时进行平移。

项目网站上:DoDragDrop - 常用拖动功能
画布网站上:OnDragOver - 平移画布

因此用户可以同时拖动和平移。

一切正常,直到我搬到新笔记本电脑(联想)和Visual Studio 2012(之前的2010年)。现在,当我按下鼠标中键(或右键)时,会立即触发画布的事件OnMouseMove。之后拖动立即停止,也没有平移。

我的同事试图从Visual Studio 2010运行相同的代码,但它运行正常。他设置了他的版本,所以我试了一下,结果是一样的 - 在我的笔记本电脑上,我不能在拖动时平移..

有没有人知道这个问题是什么? HW,SW,Lenovo,Windows?

项目信息:WPF,DevExpress 12.1,.NET 4,Windows 7 Proffesional,VS 2012

请记住,我仍然是WPF的新手:)

1 个答案:

答案 0 :(得分:2)

回答我自己的问题,也许对某人有用。

我没有发现问题出在哪里,但我的结论是,在某些电脑上,当用户在拖动时按下鼠标中键/右键,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;
        }    
}
相关问题