views:

213

answers:

1

I'm writing a small drag and drop feature on the project I'm working on. I've done this type of feature several times before, and I always use a combination of PreviewLeftMouseButtonDown and PreviewMouseMove to detect the start of a drag. It's always worked before.

On this project, though, I'm finding PreviewMouseMove is not firing when the left mouse button is clicked, even though the mouse is clearly moving on screen. I've never seen this before and this is making me sad.

This is the XAML I'm using:

<Grid Background="{StaticResource BGGradientBrush}"
      Margin="1"
      PreviewMouseLeftButtonDown="Grid_PreviewMouseLeftButtonDown"
      PreviewMouseMove="Grid_PreviewMouseMove">

      <!-- snip -->
</Grid>

And this is my code behind:

    private Point _startDragPoint;
    private bool _isDragging;

    private void Grid_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        _startDragPoint = e.GetPosition(null);
        System.Diagnostics.Trace.WriteLine("Test => (PreviewMouseLeftButtonDown) left click at " + _startDragPoint);
    }

    private void Grid_PreviewMouseMove(object sender, MouseEventArgs e)
    {
        System.Diagnostics.Trace.WriteLine("Test => (PreviewMouseMove) enter, e.LeftButton: " + e.LeftButton + ", _isDragging: " + _isDragging);

        if (e.LeftButton == MouseButtonState.Pressed && !_isDragging)
        {
            System.Diagnostics.Trace.WriteLine(
                "Test => (PreviewMouseMove) well, the left mouse button is pressed");

            var position = e.GetPosition(null);
            if (Math.Abs(position.X - _startDragPoint.X) > SystemParameters.MinimumHorizontalDragDistance ||
                Math.Abs(position.Y - _startDragPoint.Y) > SystemParameters.MinimumVerticalDragDistance)
            {
                StartDrag();
            }
        }
    }

    private void StartDrag()
    {
        System.Diagnostics.Trace.WriteLine("Test => (StartDrag) started drag!");
    }

I'm monitoring those trace statements via DebugView, and between the time Grid_PreviewMouseLeftButtonDown is fired, until the left mouse button is released, Grid_PreviewMouseMove is never fired.

I'm completely perplexed, can anyone spot something I'm doing wrong? Just to sanity check myself I've been referring to Jaime Rodriquez's Drag & Drop in WPF Explained End to End article and I'm clearly using the same technique he is, so what could possibly be causing this? I'm really hoping it's just something stupid I'm missing. Any ideas?

A: 

I'm using an Infragistics XamDataGrid to host my user controls that require drag and drop functionality. It turns out that the grid traps MouseMove events when the left mouse button is pressed. Pretty irritating. I'm not sure how to fix it or work around it, but at least I know what's going wrong now.

unforgiven3