在 Windows Presentation Foundation (WPF) 中,TextBox、RichTextBox 和 FlowDocument 控制項都有內建的拖放功能。 內建功能可在控制項內和之間啟用文字拖放功能。 不過,它不會藉由卸除在控制項上的檔案來啟用開啟檔案。 這些控制項也會將拖放事件標示為已處理。 因此,根據預設,您無法新增自己的事件處理常式,以提供開啟已卸除檔案的功能。
若要在這些控制項中新增拖放事件的其他處理,請使用 AddHandler(RoutedEvent, Delegate, Boolean) 方法來新增拖放事件的事件處理常式。 將 handledEventsToo
參數設定為 true
,為已標示為由事件路由上另一個項目處理的事件叫用指定的處理常式。
提示
您可以處理拖放事件的預覽版本,並將預覽事件標示為已處理,以取代 TextBox、RichTextBox 和 FlowDocument 的內建拖放功能。 不過,這會停用內建拖放功能,不建議這麼做。
範例
下列範例示範如何在 RichTextBox 的 DragOver 和 Drop 事件上新增處理常式。 這個範例會使用 AddHandler(RoutedEvent, Delegate, Boolean) 方法,並將 handledEventsToo
參數設定為 true
,即使 RichTextBox 將這些事件標示為已處理,也會叫用事件處理常式。 事件處理常式中的程式碼會新增功能,以開啟在 RichTextBox 上卸除的文字檔。
若要測試此範例,請將文字檔或 RTF 格式 (RTF) 檔案從 Windows 檔案總管拖曳至 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