Overview
DocumentToolBar is an Avalonia UserControl that wraps all common formatting actions.
It subscribes to RichTextEditor.SelectionInfo and automatically updates its toggle states (bold,
italic, alignment buttons, etc.) as the user moves the caret or changes the selection.
Wire it by setting the Editor property to a RichTextEditor instance.
This is typically done in your window's OnInitialized() or OnLoaded().
AXAML Usage
<DockPanel>
<pe:DocumentToolBar
x:Name="Toolbar"
DockPanel.Dock="Top"
/>
<pe:RichTextEditor
x:Name="Canvas"
/>
</DockPanel>
// In code-behind
protected override void OnInitialized()
{
base.OnInitialized();
Toolbar.Editor = Canvas;
}
Properties
| Property | Type | Description |
|---|---|---|
| Editor | RichTextEditor | The linked editor. Setting this wires up all event subscriptions. Always set this before the user interacts with the toolbar. This is an Avalonia Direct property — bindable. |
| SelectionInfo | SelectionInfo |
Reflects the current selection state (alignment, list type, list level, line spacing).
Updated automatically when the editor fires OnNavigation.
Bindable Direct property — useful for binding toggle states in custom toolbars.
|
Toolbar Controls
The toolbar is initialized during construction and includes the following controls:
Font & Size
| Control | Action |
|---|---|
| Font Family ComboBox | Populated from FontManager.Current.SystemFonts. Selection calls ApplyFontFamily(). |
| Font Size ComboBox | Sizes: 8, 9, 10, 11, 12, 14, 16, 18, 20, 24, 28, 32, 36, 48, 60, 72, 80 pt. Selection calls ApplyFontSize(). |
Style Toggles
| Button | Shortcut | Action |
|---|---|---|
| Bold (B) | Ctrl+B | DocumentController.ApplyBold() |
| Italic (I) | Ctrl+I | DocumentController.ApplyItalic() |
| Underline (U) | Ctrl+U | DocumentController.ApplyUnderline() |
| Strikethrough (S) | — | DocumentController.ApplyStrikethrough() |
| Subscript (X₂) | — | DocumentController.ApplySubscript() |
| Superscript (X²) | — | DocumentController.ApplySuperscript() |
Color
| Control | Action |
|---|---|
| Text Color button | Opens color picker → DocumentController.ApplyFontColor(color) |
| Highlight Color button | Opens color picker → DocumentController.ApplyBackgroundColor(color) |
Paragraph Alignment
| Button | Action |
|---|---|
| Align Left | ApplyAlignment(TextAlignment.Left) |
| Align Center | ApplyAlignment(TextAlignment.Center) |
| Align Right | ApplyAlignment(TextAlignment.Right) |
| Justify | ApplyAlignment(TextAlignment.Justified) |
Lists & Indent
| Button | Action |
|---|---|
| Bullet List | DocumentController.ToggleBulletList() |
| Numbered List | DocumentController.ToggleNumberedList() |
| Indent More | DocumentController.AdjustParagraphIndent(+20) |
| Indent Less | DocumentController.AdjustParagraphIndent(-20) |
Line Spacing
Dropdown with preset multipliers: 1.0, 1.15, 1.5, 2.0, 2.5, 3.0
Document Settings
Button opens DocumentSettingsDialog. Changes are applied via DocumentController.ApplyDocumentSettings().
Import & Export Methods
These methods can be called programmatically (e.g., from a menu handler) in addition to the built-in toolbar buttons.
| Method | Description |
|---|---|
async Task ImportHtml() | Opens file picker, imports HTML via HtmlImporter, replaces document content |
async Task ImportRtf() | Opens file picker, imports RTF via RtfImporter |
async Task ImportDocx() | Opens file picker, imports DOCX via OpenXmlImporter |
async Task ExportHtmlFile() | Opens save picker, exports via HtmlExporter.ExportAsync(stream) |
async Task ExportRtfFile() | Opens save picker, exports via RtfExporter.ExportAsync(stream) |
async Task ExportDocxFile() | Opens save picker, exports via OpenXmlExporter.ExportAsync(stream) |
async Task InsertImage() | Opens file picker for PNG/JPEG/BMP, calls DocumentController.InsertInlineImage() |
async Task MenuCopy() | DocumentController.Copy() |
async Task MenuCut() | DocumentController.Cut() |
async Task MenuPaste() | DocumentController.Paste() |
// Call import/export programmatically (e.g., from a custom menu)
private async void MenuImport_Click(object sender, RoutedEventArgs e)
{
await Toolbar.ImportDocx();
}
private async void MenuExport_Click(object sender, RoutedEventArgs e)
{
await Toolbar.ExportHtmlFile();
}
Automatic Selection Synchronization
When Editor is set, DocumentToolBar subscribes to the editor's
DocumentController.OnNavigation event and updates all its control states — toggle buttons,
font/size dropdowns, color displays — to match the style at the caret.
A _updatingToolbar guard flag prevents feedback loops where a toolbar update would
fire a selection-changed event that triggers another update.