DragDrop.DragOver DragDrop.DragOver DragDrop.DragOver Attached Event

定義

在置放目標項目的界限內拖曳物件時持續發生。Occurs continuously while an object is dragged within the bounds of an element that is acting as a drop target.

see AddDragOverHandler, and RemoveDragOverHandler
see AddDragOverHandler, and RemoveDragOverHandler
see AddDragOverHandler, and RemoveDragOverHandler

範例

下列範例示範 DragOver 項目的 Ellipse 事件處理常式。The following example shows the DragOver event handler for an Ellipse element. 這個程式碼會檢查是否要將 DataObject 拖曳到橢圓形上,該橢圓形包含可轉換成 Brush 的字串資料。This code checks to see if the DataObject being dragged over the ellipse contains string data that can be converted to a Brush. 如果是,則會將 DragEventArgs.Effects 屬性設定為 CopyIf so, it sets the DragEventArgs.Effects property to Copy. 這會向拖曳來源表示資料可以複製到橢圓形。This indicates to the drag source that the data can be copied to the ellipse. 如果無法將資料轉換成 Brush,則會將 DragEventArgs.Effects 屬性設定為 NoneIf the data cannot be converted to a Brush, the DragEventArgs.Effects property is set to None. 這會向拖曳來源表示橢圓形不是資料的有效置放目標。This indicates to the drag source that the ellipse is not a valid drop target for the data.

private void ellipse_DragOver(object sender, DragEventArgs e)
{
    e.Effects = DragDropEffects.None;

    // 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, allow copying.
        BrushConverter converter = new BrushConverter();
        if (converter.IsValid(dataString))
        {
            e.Effects = DragDropEffects.Copy | DragDropEffects.Move;
        }
    }
}
Private Sub Ellipse_DragOver(ByVal sender As System.Object, ByVal e As System.Windows.DragEventArgs)
    e.Effects = DragDropEffects.None

    ' 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
            e.Effects = DragDropEffects.Copy Or DragDropEffects.Move
        End If
    End If
End Sub

備註

只有在一個被拖曳的物件是做為置放目標項目的界限內時,持續,則會引發這個事件。This event is raised continuously while a dragged object is within 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.

DragOver事件會搭配GiveFeedback拖曳來源上的事件。The DragOver event is paired with the GiveFeedback event on the drag source. DragOver事件處理常式,您通常檢查傳送的資料是否在置放目標可以處理的格式。In the DragOver event handler, you typically check that the transferred data is in a format that the drop target can process. 您也可以檢查是否任何輔助按鍵按下,這通常表示使用者想在移動或複製動作。You can also check whether any modifier keys are pressed, which typically indicates whether the user intends a move or a copy action. 執行這些檢查之後,您可以設定 DragEventArgs.Effects 屬性,以通知拖曳來源有關置放資料的效果。After these checks are performed, you set the DragEventArgs.Effects property to notify the drag source what effect dropping the data will have. 拖曳來源接收資訊儲存在這個Effects屬性GiveFeedback事件引數,而且也可以設定適當的資料指標來提供回饋給使用者。The drag source receives this information in the Effects property of the GiveFeedback event arguments, and can set an appropriate cursor to give feedback to the user.

路由事件資訊Routed Event Information

識別項欄位Identifier field DragOverEvent
路由策略Routing strategy 事件反昇Bubbling
Delegate - 委派Delegate DragEventHandler

對應的通道事件是PreviewDragOverThe corresponding tunneling event is PreviewDragOver.

適用於

另請參閱