Question

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 Elementhost, 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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top