DragDrop.DoDragDrop Method
Initiates a drag-and-drop operation.
Assembly: PresentationCore (in PresentationCore.dll)
public static DragDropEffects DoDragDrop( DependencyObject dragSource, Object data, DragDropEffects allowedEffects )
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.DragDropEffectsOne of the DragDropEffects values that specifies the final effect that was performed during the drag-and-drop operation.
| Exception | Condition |
|---|---|
| ArgumentNullException |
dragSource or data is null. |
The following example demonstrates how to use the DoDragDrop method.
Private Sub DragStarted() m_IsDown = False Dim serializedObject As String = m_OriginalElement.OuterXml Dim data As DataObject = New DataObject() data.SetData(m_MyFormat.Name, serializedObject) Dim effects As DragDropEffects = _ DragDrop.DoDragDrop(MyCanvas, data, DragDropEffects.Copy Or DragDropEffects.Move) If effects And DragDropEffects.Move Then ' Remove the element. m_OriginalElement.ParentNode.RemoveChild(m_OriginalElement) m_OriginalElement = Nothing End If End Sub
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Here is a C# example using DoDragDrop to Drag Image as a File
If you want to drag any object as a file to any application that allows file drops, you can use a method like this example.
private void StartDrag(MouseEventArgs e, Image img)
{
//get the file path from a file-based image stored in WPF System.Windows.Controls.Image
Uri uriSource = new Uri(img.Source.ToString());
string fPath = Uri.UnescapeDataString(uriSource.LocalPath);
//FileDrop format requires array of file names
string[] aString = { fPath };
DataObject data = new DataObject(DataFormats.FileDrop, aString, true);
DragDropEffects de = DragDrop.DoDragDrop(img, data, DragDropEffects.Move);
}
- 6/23/2011
- Dan Randolph