[Expand]General Information
[Collapse]WinForms Controls
  .NET Core Support
  Prerequisites
 [Expand]What's Installed
 [Expand]Build an Application
 [Expand]Controls and Libraries
 [Collapse]Common Features
  [Expand]Data Binding Common Concepts
  [Expand]Data Source Wizard
  [Expand]Expressions
  [Collapse]Behaviors
    File and Folder Browser Behaviors
    Persistence Behavior
    Indicator Behaviors
    Stub Glyph Behavior
    Banner Behavior
    Breadcrumb Behavior
    Disabled Cell Behavior
    Drag-and-Drop Behavior
    Magnifier Behavior
    Pager Navigation Behavior
    Scrollbar Annotation Behavior
    Snap Window Behavior
  [Expand]Application Appearance and Skin Colors
  [Expand]Filtering UI Context
   Find Panel Syntax
  [Expand]Graphics Performance and High DPI
  [Expand]Scaffolding Wizard
  [Expand]Formatting Values
   HTML-inspired Text Formatting
  [Expand]Menus
  [Expand]Tooltips
  [Expand]Save and Restore Layouts
   Clipboard - Copy and Paste Operations. Data Formatting
   Version Compatibility: Default Property Values
  Get More Help
 [Expand]API Reference
[Expand]ASP.NET Controls and MVC Extensions
[Expand]ASP.NET Bootstrap Controls
[Expand]ASP.NET Core Bootstrap Controls
[Expand]WPF Controls
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Office File API
[Expand]Reporting
[Expand]Report and Dashboard Server
[Expand]Dashboard
[Expand]eXpressApp Framework
[Expand]eXpress Persistent Objects
[Expand]CodeRush
[Expand]CodeRush Classic
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation
View this topic on docs.devexpress.com (Learn more)

Drag-and-Drop Behavior

Expanded 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.

Note

Run the XtraTreeList or XtraGrid demo to try out drag & drop functionality.

In comparison to the standard System.Windows.Forms.Control.DoDragDrop method, the Behavior gives you the following advantages.

  • A drag operation is automatically initiated, there is no 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 cursor 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.

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).

Expanded 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.

    Important

    The attached Drag-and-Drop Behavior is disabled if the System.Windows.Forms.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 System.Windows.Forms.Control.AllowDrop property with the Behavior's DragDropBehaviorProperties.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.

DragDropBehaviorProperties.PreviewVisible

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

DragDropBehaviorProperties.InsertIndicatorVisible

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

DragDropBehaviorProperties.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.

DragDropBehaviorProperties.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 DragDropEvents.DragOver and consequent events do not raise. You can override this setting in a DragDropEvents.DragEnter event handler.

DragDropEventsName

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

Expanded 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.

Expanded 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

DragDropEvents.BeginDragDrop

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

DragDropEvents.DragEnter

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

DragDropEvents.DragOver

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

DragDropEvents.DragLeave

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

DragDropEvents.DragDrop

Occurs when a data item is dropped on the control.

DragDropEvents.EndDragDrop

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

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

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

Expanded 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.

Expanded 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.

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.

Is this topic helpful?​​​​​​​