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拖曳到橢圓形上的橢圓形包含可轉換成字串資料BrushIt 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屬性是falseThis 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.

適用於

另請參閱