Share via


DragDrop.DragEnter 附加事件

定义

当对象被拖放到作为放置目标的元素的边界内时发生。

see AddDragEnterHandler, and RemoveDragEnterHandler
see AddDragEnterHandler, and RemoveDragEnterHandler
see AddDragEnterHandler, and RemoveDragEnterHandler

示例

下面的示例演示 DragEnter 元素的 Ellipse 事件处理程序。 此代码通过保存当前的 Fill 画笔预览拖放操作的效果。 然后,它会检查拖动到椭圆上的 是否 DataObject 包含可转换为 的 Brush字符串数据。 如果是,将 Brush 应用于椭圆。 在 DragLeave 事件处理程序中还原更改。 如果数据无法转换为 Brush,则不执行任何操作。

private Brush _previousFill = null;
private void ellipse_DragEnter(object sender, DragEventArgs e)
{
    Ellipse ellipse = sender as Ellipse;
    if (ellipse != null)
    {
        // Save the current Fill brush so that you can revert back to this value in DragLeave.
        _previousFill = ellipse.Fill;
        
        // 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.
            BrushConverter converter = new BrushConverter();
            if (converter.IsValid(dataString))
            {
                Brush newFill = (Brush)converter.ConvertFromString(dataString);
                ellipse.Fill = newFill;
            }
        }
    }
}
Private _previousFill As Brush = Nothing
Private Sub Ellipse_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.DragEventArgs)
    Dim ellipse = TryCast(sender, Ellipse)
    If ellipse IsNot Nothing Then
        ' Save the current Fill brush so that you can revert back to this value in DragLeave.
        _previousFill = ellipse.Fill

        ' 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

注解

每次将对象拖动到充当放置目标的元素的边界时,都会引发此事件一次。 如果元素的 AllowDrop 属性为 false,则不会引发此事件。

对于放置目标而言,处理此事件是可选的,并不是所有拖放方案所必需的。 通常,如果适用于你的应用程序,可处理此事件,以便提供拖放操作效果预览。 请勿设置 DragEventArgs.Effects 事件中的 DragEnter 属性,因为在 DragOver 事件中该属性将被覆盖。

路由事件信息

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

相应的隧道事件为 PreviewDragEnter

适用于

另请参阅