* Reset branch for WPF changes * Convert BMP to PNG; fix link-out-of-scope err * Add snippets for WPF... 6794 files!!!! * Add missing snippets * update file updated between migration * Fix paths to include * update breadcrumb and toc * fix index links * fix index links * fix index links * fix markdown
3.2 KiB
title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
| title | ms.date | dev_langs | helpviewer_keywords | ms.assetid | |||||
|---|---|---|---|---|---|---|---|---|---|
| How to: Open a File That is Dropped on a RichTextBox Control | 03/30/2017 |
|
|
6bb8bb54-f576-41db-a9a7-24102ddeb490 |
How to: Open a File That is Dropped on a RichTextBox Control
In [!INCLUDETLA#tla_winclient], the xref:System.Windows.Controls.TextBox, xref:System.Windows.Controls.RichTextBox, and xref:System.Windows.Documents.FlowDocument controls all have built-in drag-and-drop functionality. The built-in functionality enables drag-and-drop of text within and between the controls. However, it does not enable opening a file by dropping the file on the control. These controls also mark the drag-and-drop events as handled. As a result, by default, you cannot add your own event handlers to provide functionality to open dropped files.
To add additional handling for drag-and-drop events in these controls, use the xref:System.Windows.UIElement.AddHandler%28System.Windows.RoutedEvent%2CSystem.Delegate%2CSystem.Boolean%29 method to add your event handlers for the drag-and-drop events. Set the handledEventsToo parameter to true to have the specified handler be invoked for a routed event that has already been marked as handled by another element along the event route.
Tip
You can replace the built-in drag-and-drop functionality of xref:System.Windows.Controls.TextBox, xref:System.Windows.Controls.RichTextBox, and xref:System.Windows.Documents.FlowDocument by handling the preview versions of the drag-and-drop events and marking the preview events as handled. However, this will disable the built-in drag-and-drop functionality, and is not recommended.
Example
The following example demonstrates how to add handlers for the xref:System.Windows.DragDrop.DragOver and xref:System.Windows.DragDrop.Drop events on a xref:System.Windows.Controls.RichTextBox. This example uses the xref:System.Windows.UIElement.AddHandler%28System.Windows.RoutedEvent%2CSystem.Delegate%2CSystem.Boolean%29 method and sets the handledEventsToo parameter to true so that the events handlers will be invoked even though the xref:System.Windows.Controls.RichTextBox marks these events as handled. The code in the event handlers adds functionality to open a text file that is dropped on the xref:System.Windows.Controls.RichTextBox.
To test this example, drag a text file or a rich text format (RTF) file from Windows Explorer to the xref:System.Windows.Controls.RichTextBox. The file will be opened in the xref:System.Windows.Controls.RichTextBox. If you press the SHIFT key before the dropping the file, the file will be opened as plain text.
[!code-xamlDragDropSnippets#RtbXAML]
[!code-csharpDragDropSnippets#RtbHandlers] [!code-vbDragDropSnippets#RtbHandlers]