DragDrop.DoDragDrop(DependencyObject, Object, DragDropEffects) Method

Definition

Initiates a drag-and-drop operation.

C#
[System.Security.SecurityCritical]
public static System.Windows.DragDropEffects DoDragDrop(System.Windows.DependencyObject dragSource, object data, System.Windows.DragDropEffects allowedEffects);
C#
public static System.Windows.DragDropEffects DoDragDrop(System.Windows.DependencyObject dragSource, object data, System.Windows.DragDropEffects allowedEffects);

Parameters

dragSource
DependencyObject

A reference to the dependency object that is the source of the data being dragged.

data
Object

A data object that contains the data being dragged.

allowedEffects
DragDropEffects

One of the DragDropEffects values that specifies permitted effects of the drag-and-drop operation.

Returns

One of the DragDropEffects values that specifies the final effect that was performed during the drag-and-drop operation.

Attributes

Exceptions

dragSource or data is null.

Examples

The following example shows how to initiate a drag-and-drop operation from the MouseMove event handler of an Ellipse element to make it a drag source. The transferred data is the string representation of the ellipse's Fill property. The data is passed to the DoDragDrop method as a string and is automatically wrapped in a DataObject.

C#
private void ellipse_MouseMove(object sender, MouseEventArgs e)
{
    Ellipse ellipse = sender as Ellipse;
    if (ellipse != null && e.LeftButton == MouseButtonState.Pressed)
    {
        DragDrop.DoDragDrop( ellipse,
                             ellipse.Fill.ToString(),
                             DragDropEffects.Copy);
    }
}

Remarks

It is the responsibility of your application to determine when a drag occurs, and then initiate the drag-and-drop operation. Typically, this is when a MouseDown and MouseMove sequence of events occurs over the element to be dragged. You initiate the drag-and-drop operation by calling the static DoDragDrop method and passing the transferred data to it. The DoDragDrop method will automatically wrap the data in a DataObject if necessary. For greater control over the data format, you can wrap the data in a DataObject before passing it to the DoDragDrop method.

The value returned from the DoDragDrop method is the value of the DragEventArgs.Effects property set in the drop target's Drop event handler. If the return value does not match one of the allowedEffects specified in the call to DoDragDrop, the drag-and-drop operation is not performed.

Applies to

Produkt Verzie
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10