akjoshi
akjoshi

Reputation: 15802

SIngle and Double click events not working as expected in canvas

I have a Canvas which is present in a UserControl, I have attached the DoubleClick event to this UserControl like this

mainWindow.CanvasLayout.MouseDoubleClick += 
new MouseButtonEventHandler(CanvasLayout_MouseDoubleClick);

I am using this event handler to achieve full screen functionality.

Now, Canvas can have various controls placed inside it. Drag and drop functionality is implemented for these controls similar to this codeproject article.

Basically, I handle these events for a control -

this._dragSource.PreviewMouseLeftButtonDown +=
new MouseButtonEventHandler(DragSource_PreviewMouseLeftButtonDown);

this._dragSource.PreviewMouseMove += 
new System.Windows.Input.MouseEventHandler(DragSource_PreviewMouseMove);

this._dragSource.PreviewMouseLeftButtonUp += 
new MouseButtonEventHandler(DragSource_PreviewMouseLeftButtonUp);

Now, when user DoubleClicks on a control(present in canvas) both DoubleClick(Full Screen) and single Click(drag & drop) operations are performed, i.e. if user double clicks on control and change its mouse position quickly, control position is changed(its dragged and dropped to new position).

Is there any way I can prevent drag and drop operation when user double clicks on a control?

Upvotes: 2

Views: 6223

Answers (2)

akjoshi
akjoshi

Reputation: 15802

Got it, Instead of handling MouseDoubleClick event, I used PreviewMouseLeftButtonDown -

mainWindow.CanvasLayout.PreviewMouseLeftButtonDown 
+= new MouseButtonEventHandler(CanvasLayout_PreviewMouseLeftButtonDown);

and

void CanvasLayout_PreviewMouseLeftButtonDown(object s, MouseButtonEventArgs e)
{
    if (e.ClickCount > 1)
    {
        // Do double-click code  
        // Code for FullScreen 
        e.Handled = true;
    }
}

Upvotes: 4

Nir
Nir

Reputation: 29614

What you need to do is, on mouse up start a timer the time should be retrieved from SystemInformation.DoubleClickTime, and do the click action on the timer tick (only if you didn't detect the double click in the mean time, obviously).

Likewise use SystemInformation.DragSize to prevent accidental drag&drop.

Note, the SystemInformation class is part of WinForms so you need to add a reference to System.Windows.Forms, this class will work just fine in WPF apps.

Upvotes: 0

Related Questions