Editing Text
Once a WPF window contains a TextControl, its users can type, select, copy, paste, undo and drag text without a line of code. What your application adds is control: it reads and sets the selection and the caret, puts clipboard and undo commands into its own menus, finds and replaces text, loads dropped files and reacts to keys of its choice. The pages below show how, with XAML and code-behind for each case.
How Editing Works in the WPF TextControl
- Built in. Typing, mouse and keyboard selection, the clipboard keys, undo and redo, and dragging selected text work as soon as the editor has the keyboard focus.
- In the ribbon. A window with the WPF ribbon gets a Home tab whose Clipboard group pastes, cuts and copies, and whose Editing group finds, replaces and selects text. See Built-in Ribbon Tabs.
- In code. The
TextControlof theTXTextControl.WPFnamespace has methods and properties for the same commands, such asSelect,Selection,Copy,Paste,UndoandFind. The classes they return, such asSelectionandInputPosition, belong to theTXTextControlnamespace and are shared with the Windows Forms product.
Two terms appear on every page. The input position is where typed text appears, shown by the caret. The selection is the highlighted text that the next command changes. Both are counted in text positions, starting with 0 in front of the first character.
Find the Right Page
| If you want to | Read |
|---|---|
| Select, read or replace text in code, or track the caret | Selections and Caret Position |
| Put Cut, Copy and Paste into a menu, or paste plain text | Clipboard Operations |
| Name undo steps in a menu, or group changes into one step | Undo and Redo |
| Open the Find and Replace dialog boxes or sidebars, or replace text in code | Search and Replace |
| Load files dropped from File Explorer | Drag and Drop |
| Look up a key, or handle Ctrl+F and other keys yourself | Keyboard Shortcuts |
Recommended Order
- Selections and Caret Position, because every other page works with the selection and text positions.
- The page of the command you are adding.
- Keyboard Shortcuts before you assign keys, to avoid those the editor already uses.
Pages in This Section
- Selections and Caret Position — text positions,
Select,Selection,InputPositionand the mouse actions. - Clipboard Operations —
Copy,Cut,Paste, clipboard formats and document permissions. - Undo and Redo — undo commands, action names, grouped steps and
IsUndoEnabled. - Search and Replace — dialog boxes, sidebars,
Findand replacing in all text parts. - Drag and Drop — moving text, drop formats and dropped files.
- Keyboard Shortcuts — the built-in keys and a
KeyDownhandler for your own.
Formatting selected text is covered on Text and Paragraph Formatting. Documents that are processed without being shown belong to Document Processing.