Edit

Share via


DragDrop.Drop Attached Event

Definition

Occurs when an object is dropped within the bounds of an element that is acting as a drop target.

see AddDropHandler, and RemoveDropHandler
see AddDropHandler, and RemoveDropHandler
see AddDropHandler, and RemoveDropHandler

Examples

The following example shows the Drop event handler for an Ellipse element. This code applies the effects of the drag-and-drop operation. It checks to see if the DataObject being dragged over the ellipse contains string data that can be converted to a Brush. If so, the Brush is applied to the ellipse. If the data cannot be converted to a Brush, no action is performed.

private void ellipse_Drop(object sender, DragEventArgs e)
{
    Ellipse ellipse = sender as Ellipse;
    if (ellipse != null)
    {
        // If the DataObject contains string data, extract it.
        if (e.Data.GetDataPresent(DataFormats.StringFormat))
        {
            string dataString = (string)e.Data.GetData(DataFormats.StringFormat);

            // If the string can be converted into a Brush, 
            // convert it and apply it to the ellipse.
            BrushConverter converter = new BrushConverter();
            if (converter.IsValid(dataString))
            {
                Brush newFill = (Brush)converter.ConvertFromString(dataString);
                ellipse.Fill = newFill;
            }
        }
    }
}
Private Sub Ellipse_Drop(ByVal sender As System.Object, ByVal e As System.Windows.DragEventArgs)
    Dim ellipse = TryCast(sender, Ellipse)
    If ellipse IsNot Nothing Then

        ' If the DataObject contains string data, extract it.
        If e.Data.GetDataPresent(DataFormats.StringFormat) Then
            Dim dataString = e.Data.GetData(DataFormats.StringFormat)

            ' If the string can be converted into a Brush, convert it.
            Dim converter As New BrushConverter()
            If converter.IsValid(dataString) Then
                Dim newFill As Brush = CType(converter.ConvertFromString(dataString), Brush)
                ellipse.Fill = newFill
            End If
        End If
    End If
End Sub

Remarks

The Drop event is raised once when an object is dropped within the bounds of an element that is acting as a drop target. This event is not raised if the element's AllowDrop property is false. This event ends the drag-and-drop operation.

In the Drop event handler, you extract the transferred data from the DataObject and perform any processing of the data that your application requires. To notify the drag source of the effect of the drop, such as a copy or move, set the DragEventArgs.Effects property in the Drop event handler. The value of this property is the return value of the DoDragDrop method that initiated the drag-and-drop operation. If the value that is returned does not match one of the allowedEffects specified in the call to DoDragDrop, the drag-and-drop operation is not performed. The initial value of the DragEventArgs.Effects property is the same as the allowedEffects specified in the call to the DoDragDrop method. If you do not set the DragEventArgs.Effects property, this initial value is returned and it is assumed that one the allowedEffects occurred.

Routed Event Information

Identifier field DropEvent
Routing strategy Bubbling
Delegate DragEventHandler

The corresponding tunneling event is PreviewDrop.

Applies to

See also