Silverlight DragDropTarget: Allowing dragging only when mouse left button is down

Problem:

I was using silverlight dragdroptarget to drag a row from a data grid to another. Anyone who uses dragdroptarget knows that as soon as you click on an item drag is being started no matter you release the mouse left button. I was asked to allow drag operation only when user select a row by clicking it and did not release the button. If mouse left button is up, cancel the drag.

Solution:

The solution was not so complex, just needs to be searched with patience. I was using dragdroptarget with data grids so i registered below events with my data grid.

1. You need an event in which you can catch the drag of item when it is started and a flag to determine whether to cancel the drag or not.
2. Helping events that will tell you about the mouse button state.

Main Event:
Drag Starting Event

public void dg1stUnAllocDragStarting (object sender, ItemDragEventArgs e)
    {
        if (blnMouseLeftButtonUp)
        {
            e.Cancel = true;
            e.Handled = true;
        }

        blnMouseLeftButtonUp = false;

    }

blnMouseLeftButtonUp => Global flag that tells the info about mouse state

Helping Events:

1. Mouse Left Button Up Event

public void dataGrid1stUnAllocChild_MouseLeftButtonUp (object sender, MouseButtonEventArgs e)
    {           
        blnMouseLeftButtonUp = true;

    }

2. Mouse Left Button Down Event

public void dataGrid1stUnAllocChild_MouseLeftButtonDown (object sender, MouseButtonEventArgs e)
    {           
        blnMouseLeftButtonUp = false;
        e.Handled = false;           

    }

Comments

Popular Posts