DragDrop.DragEnter DragDrop.DragEnter DragDrop.DragEnter Attached Event

定義

在置放目標項目的界限內拖曳物件時發生。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. 然後, 它會檢查拖曳到DataObject橢圓形上是否包含可以轉換成的Brush字串資料。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. 如果元素的AllowDrop屬性為, false則不會引發這個事件。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 - 委派Delegate DragEventHandler

對應的通道事件為PreviewDragEnterThe corresponding tunneling event is PreviewDragEnter.

適用於

另請參閱