Windows Presentation Foundation (WPF) では、TextBox、RichTextBox、および FlowDocument コントロールには、すべてドラッグ アンド ドロップ機能が組み込まれています。 組み込みの機能により、コントロール内とコントロール間でテキストをドラッグ アンド ドロップできます。 ただし、コントロール上のファイルをドロップしてファイルを開く機能はありません。 これらのコントロールでは、ドラッグ アンド ドロップ イベントも処理済みとしてマークされます。 その結果、既定では、ドロップされたファイルを開く機能を提供する独自のイベント ハンドラーを追加することはできません。
これらのコントロールにドラッグ アンド ドロップ イベントの処理を追加するには、AddHandler(RoutedEvent, Delegate, Boolean) メソッドを使用して、ドラッグ アンド ドロップ イベントのイベント ハンドラーを追加します。
handledEventsToo
パラメーターを true
に設定して、イベント ルートに沿って別の要素によって既に処理済みとしてマークされているルーティング イベントに対して指定されたハンドラーを呼び出すようにします。
ヒント
ドラッグ アンド ドロップ イベントのプレビュー バージョンを処理し、プレビュー イベントを処理としてマークすることで、TextBox、RichTextBox、および FlowDocument の組み込みのドラッグ アンド ドロップ機能を置き換えることができます。 ただし、これにより組み込みのドラッグ アンド ドロップ機能が無効になり、推奨されません。
例
次の例では、DragOverに Drop イベントと RichTextBox イベントのハンドラーを追加する方法を示します。 この例では、AddHandler(RoutedEvent, Delegate, Boolean) メソッドを使用し、handledEventsToo
がこれらのイベントを処理対象としてマークした場合でもイベント ハンドラーが呼び出されるように、true
パラメーターを RichTextBox に設定します。 イベント ハンドラーのコードは、RichTextBoxで削除されたテキスト ファイルを開く機能を追加します。
この例をテストするには、Windows エクスプローラーからテキスト ファイルまたはリッチ テキスト形式 (RTF) ファイルを RichTextBoxにドラッグします。 ファイルが RichTextBox 内で開かれます。 ファイルをドロップする前に Shift キーを押すと、ファイルはプレーン テキストとして開かれます。
<RichTextBox x:Name="richTextBox1"
AllowDrop="True" />
public MainWindow()
{
InitializeComponent();
// Add using System.Windows.Controls;
richTextBox1.AddHandler(RichTextBox.DragOverEvent, new DragEventHandler(RichTextBox_DragOver), true);
richTextBox1.AddHandler(RichTextBox.DropEvent, new DragEventHandler(RichTextBox_Drop), true);
}
private void RichTextBox_DragOver(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effects = DragDropEffects.All;
}
else
{
e.Effects = DragDropEffects.None;
}
e.Handled = false;
}
private void RichTextBox_Drop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] docPath = (string[])e.Data.GetData(DataFormats.FileDrop);
// By default, open as Rich Text (RTF).
var dataFormat = DataFormats.Rtf;
// If the Shift key is pressed, open as plain text.
if (e.KeyStates == DragDropKeyStates.ShiftKey)
{
dataFormat = DataFormats.Text;
}
System.Windows.Documents.TextRange range;
System.IO.FileStream fStream;
if (System.IO.File.Exists(docPath[0]))
{
try
{
// Open the document in the RichTextBox.
range = new System.Windows.Documents.TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd);
fStream = new System.IO.FileStream(docPath[0], System.IO.FileMode.OpenOrCreate);
range.Load(fStream, dataFormat);
fStream.Close();
}
catch (System.Exception)
{
MessageBox.Show("File could not be opened. Make sure the file is a text file.");
}
}
}
}
Public Sub New()
InitializeComponent()
richTextBox1.AddHandler(RichTextBox.DragOverEvent, New DragEventHandler(AddressOf RichTextBox_DragOver), True)
richTextBox1.AddHandler(RichTextBox.DropEvent, New DragEventHandler(AddressOf RichTextBox_Drop), True)
End Sub
Private Sub RichTextBox_DragOver(sender As Object, e As DragEventArgs)
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
e.Effects = DragDropEffects.All
Else
e.Effects = DragDropEffects.None
End If
e.Handled = False
End Sub
Private Sub RichTextBox_Drop(sender As Object, e As DragEventArgs)
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
Dim docPath As String() = TryCast(e.Data.GetData(DataFormats.FileDrop), String())
' By default, open as Rich Text (RTF).
Dim dataFormat = DataFormats.Rtf
' If the Shift key is pressed, open as plain text.
If e.KeyStates = DragDropKeyStates.ShiftKey Then
dataFormat = DataFormats.Text
End If
Dim range As TextRange
Dim fStream As IO.FileStream
If IO.File.Exists(docPath(0)) Then
Try
' Open the document in the RichTextBox.
range = New TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd)
fStream = New IO.FileStream(docPath(0), IO.FileMode.OpenOrCreate)
range.Load(fStream, dataFormat)
fStream.Close()
Catch generatedExceptionName As System.Exception
MessageBox.Show("File could not be opened. Make sure the file is a text file.")
End Try
End If
End If
End Sub
.NET Desktop feedback