开发者

How do you implement drag and drop from a small WPF Element host in a Winforms application?

In our app we host a small WPF Listbox inside of an Element host, we implement drag and drop using the PreviewMouseDown event...

   private void Border_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            // Get the current mouse position
            Point mousePos = e.GetPosition(null);
            Vector diff = startPoint - mousePos;

            if (e.LeftButton == MouseButtonState.Pressed &&
                Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance &&
                Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance)
            {
                OnDragStarted(e);                
            } 

        }

The problem we are seeing is that when I click and drag an item fairly quickly, the WPF control only fires one PreviewMouseMove event before the mouse leaves the Element开发者_StackOverflow社区host, therefore the drag operation is not started until the mouse is returned to the Elementhost and another PreviewMouseMove event is raised.

Is there a robust way of handling this case?


You have to capture the mouse on the mouse down event. Any mouse moves after that are always routed to your window, even if the cursor is no longer hovering it. Use the Mouse.Capture() method in WPF.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜