Share via


DragDrop.DragEnter 添付イベント

定義

オブジェクトが、ドロップ先として機能する要素の境界内までドラッグされた時点で発生します。

see AddDragEnterHandler, and RemoveDragEnterHandler
see AddDragEnterHandler, and RemoveDragEnterHandler
see AddDragEnterHandler, and RemoveDragEnterHandler

次の例は、Ellipse 要素の DragEnter イベント ハンドラーを示しています。 このコードでは、現在の 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

注釈

このイベントは、ドロップ ターゲットとして機能する要素の境界にオブジェクトがドラッグされるたびに 1 回発生します。 要素 AllowDrop の プロパティが の場合、このイベントは false発生しません。

このイベントの処理はドロップ ターゲットでは省略可能であり、すべてのドラッグ アンド ドロップ シナリオでは必要ありません。 通常は、アプリケーションに適していれば、ドラッグ アンド ドロップ操作の効果のプレビューを提供するように、このイベントを処理します。 DragEventArgs.Effects イベントで DragEnter プロパティを設定しないでください。このプロパティは DragOver イベントで上書きされるためです。

ルーティングされたイベント情報

識別子フィールド DragEnterEvent
ルーティング戦略 バブル
代理人 DragEventHandler

対応するトンネリング イベントは です PreviewDragEnter

適用対象

こちらもご覧ください