Drag-and-Drop Behavior

  • 6 minutes to read

Overview

The Drag-and-Drop Behavior can be attached to a control that displays data to allow users to move/reorder data items with the mouse.

Drag-and-drop Grid

TIP

Run the XtraTreeList or XtraGrid demo to preview the drag-and-drop feature. Click Open Solution in the ribbon for the source code.

The Drag-and-Drop Behavior has the following advantages:

  • A drag operation is automatically initiated. You do not need to handle mouse events.
  • The event arguments already include data items being dragged.
  • A preview of the dragged element(s) is displayed during the drag operation.
  • The mouse pointer indicates whether the user can drop items at the current position.
  • An insert indicator shows the place where data items will be inserted.

Supported controls

The Behavior can be attached to the following controls:

NOTE

With this Behavior, you can drag items between supported controls only. To drag items between any controls, use the standard drag-and-drop engine. See the following help topic for information: Drag-and-Drop Functionality in Windows Forms.

Typical scenarios

Reorder data items

To allow users to reorder data items within a single control, attach the Behavior to that control. The tree list and list box controls automatically support reordering if the Behavior is attached. The grid control requires to handle drag events to show a preview of the dragged elements, the insert indicator, and to reorder data items in the data source (see an example).

Move data items

To allow users to move data items between two controls (from one grid to another, from a grid to a tree list, etc.), attach the Behavior to both controls. If both grids/tree lists have the same columns, there are no more actions required. If data structure differs, you need to handle drag events to convert data items (see the XtraTreeList demo).

To allow users to move data items between detail views in a grid control with master-detail data presentation, attach the Drag-and-Drop Behavior to the main and detail views (see DragDropEvents for an example).

How to Attach the Drag-and-Drop Behavior to a Control in the Designer

To attach the Drag-and-Drop Behavior to a control, do the following.

  • In Visual Studio's Toolbox, find the BehaviorManager component and drop it on your form.
  • Use the component's smart tag menu, to invoke the editor.
  • Use the editor's drop-down menu, to add the Drag-and-Drop Behavior.
  • The Target property is automatically set to a control on the form that supports the Behavior. You can change the target control.

    BehavoirManager_Designer

    IMPORTANT

    The attached Drag-and-Drop Behavior is disabled if the Control.AllowDrop property is set to true. It is assumed that you use the standard drag-and-drop engine in this case.

    Do not confuse the Control's AllowDrop property with the Behavior's AllowDrop property.

In the Behavior editor's Properties section, you can specify the following options.

Option

Description

Target

Specifies the control/view to which the Behavior is attached.

PreviewVisible

Specifies whether a preview of dragged element(s) is displayed during the operation.

InsertIndicatorVisible

Specifies whether to show where data items will be inserted. You can customize the indicator in a DragOver event handler.

AllowDrag

Specifies whether users are allowed to drag data items from the control. Set this property to false if you want the control to only be a target of drag operations.

AllowDrop

Specifies whether users are allowed to drop data items on the control. Set this property to false if you want the control to only be a source of drag operations. If this setting is set to false, the DragOver and consequent events do not raise. You can override this setting in a DragEnter event handler.

DragDropEventsName

Specifies the name of the DragDropEvents component that you can use to subscribe to drag events (see below).

How to Attach the Drag-and-drop Behavior to a Control in Code

To assign the Behavior to a control in code, use the BehaviorManager.Attach method. To remove the Behavior, use the BehaviorManager.Detach method. The code below shows how to assign the Behavior to two grids, specify options, and add event handlers.

using DevExpress.Utils.Behaviors;
using DevExpress.Utils.DragDrop;

BehaviorManager behaviorManager1 = new BehaviorManager(this.components);

//To assign the Behavior to a control in code, use the Attach method.
behaviorManager1.Attach<DragDropBehavior>(gridView1, behavior => {
    behavior.Properties.AllowDrop = true;
    behavior.Properties.InsertIndicatorVisible = true;
    behavior.Properties.PreviewVisible = true;
    behavior.DragDrop += Behavior_DragDropToGrid1;
});

behaviorManager1.Attach<DragDropBehavior>(gridView2, behavior => {
    behavior.Properties.AllowDrop = false;
    behavior.Properties.InsertIndicatorVisible = false;
    behavior.Properties.PreviewVisible = false;
    behavior.DragDrop += Behavior_DragDropToGrid2;
});

//To remove the Behavior, use the Detach method.
behaviorManager1.Detach<DragDropBehavior>(gridView1);
behaviorManager1.Detach<DragDropBehavior>(gridView2);

How to Customize Drag-and-Drop Operations

To customize drag-and-drop operations, handle the events that fire at the following stages.

  • The user starts to drag an item.
  • The mouse pointer enters, leaves, or moves over a control.
  • The user completes the operation (drops the element).

The DragDropEvents component is automatically added to your form when you attach the Behavior to a control in the designer. You can find its name in the Behavior editor's Properties section. Use this component to subscribe to the following events.

Event

Description

BeginDragDrop

Occurs when a drag-and-drop operation is initiated.

DragEnter

Occurs when a data item is dragged into the control's bounds

DragOver

Occurs when a data item is dragged over the control's bounds

DragLeave

Occurs when a data item is dragged out of the control's bounds.

DragDrop

Occurs when a data item is dropped on the control.

EndDragDrop

Occurs when a drag-and-drop operation is completed.

You can add event handlers in the designer or in code.

DragndropEvents

dragDropEvents1.DragDrop += dragDropEvents1_DragDrop;
dragDropEvents1.DragOver += dragDropEvents1_DragOver;

If you attach the Behavior in code, the DragDropEvents component is not added to the form. Use the Behavior's events in this case.

behaviorManager1.Attach<DragDropBehavior>(gridView1, behavior => {
    behavior.DragDrop += Behavior_DragDropToGrid2;
});

Drag-and-Drop Manager

The static (Shared in VB) DragDropManager.Default property provides access to the Drag-and-Drop Behavior manager. You can also use this property to subscribe to drag-and-drop events in a centralized way or specify settings. See the XtraTreeList demo for an example.

Business Object Requirements

Business objects must have a default constructor with no parameters if the Behavior is attached to data-aware controls that store these objects defined in code. Otherwise, rows that display these objects cannot be moved between controls.

If a business object does not have a default constructor, you must handle the target control's DragOver and DragDrop events to manually move/copy the dragged item. See DragDrop for an example.

NOTE

The Behavior copies data-aware controls' data, not underlying data source records. If a data object has fields that are not associated with, for instance, source Data Grid columns, values of these fields will be lost.