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

適用於

另請參閱