DragDrop.Drop 附加事件

定义

在将对象拖入用作放置目标的元素边界时发生。

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

示例

下面的示例演示 Drop 元素的 Ellipse 事件处理程序。 此代码应用拖放操作的效果。 它会检查是否将 DataObject 拖动到包含可以转换为 Brush 的字符串数据的椭圆上方。 如果是,将 Brush 应用于椭圆。 如果数据无法转换为 Brush,则不执行任何操作。

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

注解

Drop 对象被放置到充当放置目标的元素的边界内时,将引发该事件一次。 如果元素的 AllowDrop 属性为 false,则不会引发此事件。 此事件结束拖放操作。

在事件处理程序中 Drop ,从 DataObject 中提取传输的数据,并执行应用程序所需的数据的任何处理。 若要通知拖动源放置的效果(如复制或移动),请在 事件处理程序中Drop设置 DragEventArgs.Effects 属性。 此属性的值是启动拖放操作的方法的返回值 DoDragDrop 。 如果返回的值与对 DoDragDropallowedEffects调用中指定的值之一不匹配,则不执行拖放操作。 属性的初始值DragEventArgs.Effects与调用DoDragDrop方法中指定的 相同allowedEffects。 如果未设置 DragEventArgs.Effects 属性,则返回此初始值,并假定发生了 一 allowedEffects 个 。

路由事件信息

标识符字段 DropEvent
路由策略 鼓 泡
委托 DragEventHandler

相应的隧道事件为 PreviewDrop

适用于

另请参阅