Сначала я сделал 2 новых обработчика событий в конструкторе
public MainWindow()
{
InitializeComponent();
this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
RichTextBoxForOpenText.AddHandler(RichTextBox.DragOverEvent, new DragEventHandler(RichTextBox_DragOver), true);
RichTextBoxForOpenText.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(System.Windows.DataFormats.FileDrop))
{
GetTextFromDroppetFile(e);
}
}
И это мой метод для извлечениятекст из одного из пропущенных файлов.
private void GetTextFromDroppetFile(DragEventArgs e)
{
//Die RichTextBox wird hier erstmal gecleart
RichTextBoxForOpenText.Document.Blocks.Clear();
string[] filenames = e.Data.GetData(System.Windows.DataFormats.FileDrop) as string[];
foreach (var name in filenames)
{
RichTextBoxForOpenText.AppendText(File.ReadAllText(name));
}
}