2
votes

I have a ListView with StackPanel as ListView Items. The StackPanel has a contextmenu which is displayed on Right Click. I can rearrange the StackPanel order using drag and drop operation.

The problem is, when I select and Right click on a StackPanel (ListViewItem) context menu is displayed, then I click on another StackPanel (other ListViewItem), DragEnter, DragOver and DragLeave events are getting triggered.

Simple Right and Left click operation is interpreted as Drag Drop. I tried setting mouse right button Up and Down event to handled true but no use. How can I differentiate this scenario with real drag drop operation?

2

2 Answers

0
votes

If you're not doing this already ... you can store the initial mouse position on the PreviewMouseLeftButtonDown and use PreviewMouseMove to determine if the user is doing a drag drop operation. Here's a bit of sample code:

private static void OnPreviewMouseLeftButtonDown( object sender, MouseButtonEventArgs e )
    {
        // Store the mouse position
        m_StartPoint = e.GetPosition( null );
    }

and

private static void OnPreviewMouseMove( object sender, MouseEventArgs e )
    {
        Point mousePos = e.GetPosition( null );
        Vector diff = m_StartPoint - mousePos;

        if ( e.LeftButton == MouseButtonState.Pressed &&
            ( Math.Abs( diff.X ) > SystemParameters.MinimumHorizontalDragDistance || Math.Abs( diff.Y ) > SystemParameters.MinimumVerticalDragDistance ) )
        {
            // Dragging ...
        }
    }
0
votes

I think you can also try timing how long the mouse has been pressed. That seems to be how my iphone knows when I'm trying to move applications around.

I would post code, but this turned out to be a lot more difficult than I expected. Apparently, I can't assign mouseTimer.Elapsed to an event in in the MainWindow() method, start the timer in the MouseLeftButtonDown(s, e) method, and stop the timer in the MouseLeftButtonUp(s, e) method. Something about them using different threads and needing a Dispatcher. If you understand threading (I don't), it should be fairly easy to get this working. The goal is to check if the left mouse button is pressed when the Elapsed event happens. That means the person held the mouse button down long enough (or touched the thing long enough if it's a touch screen).