Skip to content
slluis edited this page Mar 16, 2012 · 7 revisions

This document explains how Drag & Drop works in XWT, and which methods and events are involved.

Starting a drag operation automatically

A widget can be configured as a drag source, so XWT will start a drag operation when the user clicks and drags on the widget.

Drag Source
1

The source calls Widget.SetDragSource, which sets a widget as an automatic drag source. For example:

someWidget.SetDragSource (TransferDataType.Text);
2

When the user clicks and drags on the widget the DragStarted is fired on the widget. The drag operation can be set up in the handler for the event. For example:

someWidget.SetDragSource (TransferDataType.Text);
someWidget.DragStarted += delegate (object sender, DragStartedEventArgs args) {
    var dragOper = args.DragOperation;
    dragOper.Data.AddValue ("Hi there!");
    var img = Image.FromResource (GetType(), "someNiceIcon.png");
    dragOper.SetDragImage (img, 0, 0);
    dragOper.AllowedActions = DragDropAction.All;
}

Starting a drag operation manually

This procedure is useful when you want to have more control on when and how a drag operation is started.

Drag Source
1

The source calls Widget.CreateDragOperation to start a new drag operation. This method returns a DragOperation object which can be used to set the data to be dragged, and the drag icon to be used. For example:

var dragOper = someWidget.CreateDragOperation ();
dragOper.Data.AddValue ("Hi there!");
var img = Image.FromResource (GetType(), "someNiceIcon.png");
dragOper.SetDragImage (img, 0, 0);
dragOper.AllowedActions = DragDropAction.All;
dragOper.Start ();

Dragging over a target

This sequence starts when the users moves the mouse over a widget while dragging.

Drag Source Drop Target
1

The DragOverCheck is raised.

The handler of the event can specify in AllowedAction which drop operations are supported, or None if drop is not supported. If AllowedAction is set to something other than Default the mouse cursor is updated to reflect the new state and the sequence ends here (DragOver won't be raised in this case)

2 If the DragOver event is subscribed in the target, XWT retrieves the data from the source. If the DataRequestCallback was set in the drag data source, it will be invoked now.
3

The DragOver event is raised.

The handler of the event must specify in AllowedAction which drop operations are supported, or None if drop is not supported. The mouse cursor is updated to reflect the new state.

4 At any point of the sequence, if the mouse is moved outside of the widget the DragLeave event is raised.

Dropping

This sequence starts when the user releases the mouse button while over the target widget.

Drag Source Drop Target
2 XWT retrieves the drag data from the source. If the DataRequestCallback was set in the drag data source, it will be invoked now. All this is only done if the data was not already retrieved in a DragOver event.
3

The DragDrop event is raised.

The handler must set the Success property to True if the drop operation was successfully completed.

4

The Finished event is raised in the source drag operation object.

The DeleteSource property of the event arguments is set to True if the drag operation was a Move.

Clone this wiki locally