开发者

WPF Drag and Drop - PreviewMouseMove + PreviewMouseLeftButtonDown cant work together?

If I am using two event handlers, PreviewMouseLeftButtonDown and PreviewMouseMove. The problem I am having is that everything is working fine when PreviewMouseLEftButtonDown is triggered, but since it is a drag and drop operation, the left button stays down. So while they are holding the left button down, the PreviewMouseMove EventHandler should be handling it, but it is not being called until after they release the Left mouse button

Here is what is being called first

 private开发者_如何学运维 void FieldItemGrid_PreviewMouseLeftButtonDown(object sender, MouseEventArgs e)
    {
        down = e.GetPosition(this);
        Grid fieldItemGrid = (Grid)sender;

        fieldItemGrid.Background = Brushes.White;
        _isDown = true;
        _startPoint = e.GetPosition(this);
        _originalElement = fieldItemGrid;
        this.CaptureMouse();
        e.Handled = true;


        _selectedElement = fieldItemGrid;
        DragStarted(e.GetPosition(this));
    }

Everything is working fine in here, but the problem is, if they are moving the mouse while holding, it is not executing the following the handler for PreviewMouseMove

 private void FieldItemGrid_PreviewMouseMove(object sender, MouseEventArgs e)
    {

        if (_isDown)
        {
            if (_selectedElement != null)
            {
                DragDrop.DoDragDrop(_selectedElement, _selectedElement, DragDropEffects.Move);
            }
            if (_isDragging)
            {
                DragMoved(e.GetPosition(this));
            }

        }
    }

Is there a way around this? So that I am not blocked from hitting other event handlers before I release the Left Mouse Button?


Unless this is the Grid, saying this.CaptureMouse() will prevent any other element including the Grid from receiving mouse events. For drag-and-drop you probably don't need to capture the mouse at all, but if you do capture the mouse, you need to use fieldItemGrid.CaptureMouse() in order for your mouse move handler to be called before the capture is released.


It looks like this question has been answered for a while, so I won't elaborate too much, but for the sake of others running into the same problem, you can also implement drag and drop using the DragDrop.DoDragDrop() method in code-behind on the source when you click the mouse button. On the target, you set "AllowDrop" to true in XAML and handle the "Drop" method. Your implementation will vary, but will look something like:

private void MyUIElement_PreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        //find the clicked row
        var tempObject = e.Source as [whatever];
        if (tempObject == null) return;

        DragDrop.DoDragDrop(tempObject, tempObject.PropertyToBePassed, DragDropEffects.Copy);
    }

and then in the "Drop" event handler in the target you will have something like (passing a string property for the sake of an example):

private void MyTarget_Drop(object sender, DragEventArgs e)
    {
        string myNewItem = (string)e.Data.GetData(DataFormats.StringFormat);
        Debug.WriteLine("I just received: " + myNewItem);
    }

You still don't capture the mouse events, but it's a quick and easy way to accomplish the same thing in many cases.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜