DragDrop.DragEnter 附加事件

定义

当对象被拖放到作为放置目标的元素的边界内时发生。Occurs when an object is dragged into the bounds of an element that is acting as a drop target.

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

示例

下面的示例演示 DragEnter 元素的 Ellipse 事件处理程序。The following example shows the DragEnter event handler for an Ellipse element. 此代码通过保存当前的 Fill 画笔预览拖放操作的效果。This code previews the effects of the drag-and-drop operation by saving the current Fill brush. 然后,它会检查要拖动到椭圆上的 @no__t 0 是否包含可以转换为 @no__t 的字符串数据。It then checks to see if the DataObject being dragged over the ellipse contains string data that can be converted to a Brush. 如果是,将 Brush 应用于椭圆。If so, the Brush is applied to the ellipse. DragLeave 事件处理程序中还原更改。The change is reverted in the DragLeave event handler. 如果数据无法转换为 Brush,则不执行任何操作。If the data cannot be converted to a Brush, no action is performed.

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

注解

每次将对象拖到作为拖放目标的元素的边界中时,将引发此事件。This event is raised once each time an object is dragged into the bounds of an element that is acting as a drop target. 如果元素的 @no__t 0 属性 @no__t 为-1,则不会引发此事件。This event is not raised if the element's AllowDrop property is false.

对于放置目标,处理此事件是可选的,对于所有拖放方案,此事件不是必需的。Handling this event is optional for the drop target, and is not necessary for all drag-and-drop scenarios. 通常,如果适用于你的应用程序,可处理此事件,以便提供拖放操作效果预览。You typically handle this event to provide a preview of the effects of the drag-and-drop operation, if appropriate for your application. 请勿设置 DragEventArgs.Effects 事件中的 DragEnter 属性,因为在 DragOver 事件中该属性将被覆盖。Do not set the DragEventArgs.Effects property in the DragEnter event, as it will be overwritten in the DragOver event.

路由事件信息Routed Event Information

标识符字段Identifier field DragEnterEvent
路由策略Routing strategy 冒泡Bubbling
委托Delegate DragEventHandler

对应的隧道事件为 PreviewDragEnterThe corresponding tunneling event is PreviewDragEnter.

适用于

另请参阅