Nasıl yapılır: RichTextBox Denetimine Bırakılan bir Dosyayı Açma
Windows Presentation Foundation (WPF) içinde,, TextBoxRichTextBox ve FlowDocument denetimlerinin hepsi yerleşik sürükle ve bırak işlevselliğine sahiptir. Yerleşik işlevsellik, denetimin içindeki ve içindeki metnin sürükleme ve bırakma işlevlerini sunar. Ancak, dosyayı denetimde bırakarak bir dosyayı açmayı etkinleştirmez. Bu denetimler Ayrıca sürükle ve bırak olaylarını işlenmiş olarak işaretler. Sonuç olarak, bırakılan dosyaları açan işlevselliği sağlamak için varsayılan olarak kendi olay işleyicilerinizi ekleyemezsiniz.
Bu denetimlerde sürükle ve bırak olaylarına ek işleme eklemek için, AddHandler(RoutedEvent, Delegate, Boolean) sürükle ve bırak olayları için olay işleyicilerinizi eklemek üzere yöntemini kullanın. Parametresi, handledEventsTootrue daha önce olay rotası boyunca başka bir öğe tarafından işlenmiş olarak işaretlenmiş bir yönlendirilmiş olay için belirtilen işleyicinin çağrılması için olarak ayarlayın.
İpucu
Sürükle ve bırak TextBoxRichTextBoxFlowDocument olaylarının önizleme sürümlerini işleyerek ve önizleme olaylarını işlenmiş olarak işaretleyerek, ve ' nin yerleşik sürükle ve bırak işlevini değiştirebilirsiniz. Bununla birlikte, bu, yerleşik sürükle ve bırak işlevlerini devre dışı bırakır ve önerilmez.
Örnek
Aşağıdaki örnek, içindeki ve olayları için işleyicilerin nasıl ekleneceğini DragOver gösterir DropRichTextBox . Bu örnek, AddHandler(RoutedEvent, Delegate, Boolean) yöntemini kullanır ve handledEventsTootrueRichTextBox Bu olayları işlenmiş olarak işaretlese de olayları işleyicileri çağrılacak şekilde parametresini olarak ayarlar. Olay işleyicilerindeki kod, üzerinde bırakılan bir metin dosyasını açmak için işlevsellik ekler RichTextBox .
bu örneği test etmek için, bir metin dosyası veya Windows gezgini ' nden bir zengin metin biçimi (RTF) dosyasını ' a sürükleyin RichTextBox . Dosya içinde açılır RichTextBox . Dosyayı bırakmadan önce SHIFT tuşuna basarsanız dosya düz metin olarak açılır.
<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