DragDrop.Drop 附加事件

定義

在置放目標項目的界限內置放物件時發生。

see AddDropHandler, and RemoveDropHandler
see AddDropHandler, and RemoveDropHandler
see AddDropHandler, and RemoveDropHandler

範例

下列範例示範 Drop 項目的 Ellipse 事件處理常式。 此程式碼會套用拖放作業的效果。 它會檢查是否要將 DataObject 拖曳到橢圓形上,該橢圓形包含可轉換成 Brush 的字串資料。 如果是,則會將 Brush 套用至橢圓形。 如果無法將資料轉換成 Brush,則不會執行任何動作。

private void ellipse_Drop(object sender, DragEventArgs e)
{
    Ellipse ellipse = sender as Ellipse;
    if (ellipse != null)
    {
        // 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 and apply it to the ellipse.
            BrushConverter converter = new BrushConverter();
            if (converter.IsValid(dataString))
            {
                Brush newFill = (Brush)converter.ConvertFromString(dataString);
                ellipse.Fill = newFill;
            }
        }
    }
}
Private Sub Ellipse_Drop(ByVal sender As System.Object, ByVal e As System.Windows.DragEventArgs)
    Dim ellipse = TryCast(sender, Ellipse)
    If ellipse IsNot Nothing Then

        ' 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

備註

Drop當物件在做為置放目標的元素界限內卸載時,就會引發事件一次。 如果專案的 AllowDrop 屬性為 false ,則不會引發這個事件。 此事件會結束拖放作業。

在 事件處理常式中 Drop ,您會從 DataObject 擷取傳輸的資料,並執行應用程式所需的任何資料處理。 若要通知拖放來源的效果,例如複製或移動,請在事件處理常式中 Drop 設定 DragEventArgs.Effects 屬性。 這個屬性的值是起始拖放作業之方法的傳回值 DoDragDrop 。 如果傳回的值不符合 呼叫 DoDragDrop 中指定的其中 allowedEffects 一個 ,則不會執行拖放作業。 屬性的初始值 DragEventArgs.Effects 與呼叫 DoDragDrop 方法中指定的 值相同 allowedEffects 。 如果您未設定 DragEventArgs.Effects 屬性,則會傳回這個初始值,並假設發生這個 allowedEffects 值。

路由事件資訊

識別碼欄位 DropEvent
路由策略 鼓 泡
代理人 DragEventHandler

對應的通道事件為 PreviewDrop

適用於

另請參閱