共用方式為


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

適用於

另請參閱