DragDrop.DoDragDrop(DependencyObject, Object, DragDropEffects) 方法

定義

啟始拖放作業。

public:
 static System::Windows::DragDropEffects DoDragDrop(System::Windows::DependencyObject ^ dragSource, System::Object ^ data, System::Windows::DragDropEffects allowedEffects);
[System.Security.SecurityCritical]
public static System.Windows.DragDropEffects DoDragDrop (System.Windows.DependencyObject dragSource, object data, System.Windows.DragDropEffects allowedEffects);
public static System.Windows.DragDropEffects DoDragDrop (System.Windows.DependencyObject dragSource, object data, System.Windows.DragDropEffects allowedEffects);
[<System.Security.SecurityCritical>]
static member DoDragDrop : System.Windows.DependencyObject * obj * System.Windows.DragDropEffects -> System.Windows.DragDropEffects
static member DoDragDrop : System.Windows.DependencyObject * obj * System.Windows.DragDropEffects -> System.Windows.DragDropEffects
Public Shared Function DoDragDrop (dragSource As DependencyObject, data As Object, allowedEffects As DragDropEffects) As DragDropEffects

參數

dragSource
DependencyObject

相依性物件的參考,此物件為要拖曳之資料的來源。

data
Object

包含要拖曳之資料的資料物件。

allowedEffects
DragDropEffects

其中一個 DragDropEffects 值,這個值會指定拖放作業允許的效果。

傳回

其中一個 DragDropEffects 值,這個值會指定在拖放作業期間執行的最終效果。

屬性

例外狀況

dragSourcedatanull

範例

下列範例示範如何從 MouseMove 項目的 Ellipse 事件處理常式啟始拖放作業,使該項目成為拖曳來源。 傳輸的資料是橢圓屬性的 Fill 字串表示。 資料會以字串的形式傳遞至 DoDragDrop 方法,並會自動包裝在 中 DataObject

private void ellipse_MouseMove(object sender, MouseEventArgs e)
{
    Ellipse ellipse = sender as Ellipse;
    if (ellipse != null && e.LeftButton == MouseButtonState.Pressed)
    {
        DragDrop.DoDragDrop( ellipse,
                             ellipse.Fill.ToString(),
                             DragDropEffects.Copy);
    }
}
Private Sub Ellipse_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseEventArgs)
    Dim ellipse = TryCast(sender, Ellipse)
    If ellipse IsNot Nothing AndAlso e.LeftButton = MouseButtonState.Pressed Then
        DragDrop.DoDragDrop(ellipse, ellipse.Fill.ToString(), DragDropEffects.Copy)
    End If
End Sub

備註

應用程式負責判斷何時發生拖曳,然後起始拖放作業。 一般而言,這是 MouseDown 在要拖曳的 元素上發生 和 MouseMove 事件序列時。 您可以藉由呼叫靜態 DoDragDrop 方法並將傳輸的資料傳遞給它,來起始拖放作業。 DoDragDrop 方法會視需要自動將資料包裝在 DataObject 中。 為了進一步控制資料格式,您可以將資料包裝在 DataObject 中,再傳遞給 DoDragDrop 方法。

DoDragDrop 方法傳回的值是置放目標 Drop 事件處理常式中設定的屬性值 DragEventArgs.Effects 。 如果傳回值不符合 呼叫 DoDragDrop 中指定的其中 allowedEffects 一個 ,則不會執行拖放作業。

適用於