DragDrop.DoDragDrop Method

Initiates a drag-and-drop operation.

Namespace:  System.Windows
Assembly:  PresentationCore (in PresentationCore.dll)

'Declaration
Public Shared Function DoDragDrop ( _
	dragSource As DependencyObject, _
	data As Object, _
	allowedEffects As DragDropEffects _
) As DragDropEffects

Parameters

dragSource
Type: System.Windows.DependencyObject

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

data
Type: System.Object

A data object that contains the data being dragged.

allowedEffects
Type: System.Windows.DragDropEffects

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

Return Value

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

ExceptionCondition
ArgumentNullException

dragSource or data is Nothing.

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.

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.

Private Sub Ellipse_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseEventArgs)
    Dim ellipse = TryCast(sender, Ellipse)
    If ellipse IsNot Nothing AndAlso e.LeftButton = MouseButtonState.Pressed Then
        DragDrop.DoDragDrop(ellipse, ellipse.Fill.ToString(), DragDropEffects.Copy)
    End If 
End Sub

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft