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

PropertyTypeDescription
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

ControlAction
Font Family ComboBoxPopulated from FontManager.Current.SystemFonts. Selection calls ApplyFontFamily().
Font Size ComboBoxSizes: 8, 9, 10, 11, 12, 14, 16, 18, 20, 24, 28, 32, 36, 48, 60, 72, 80 pt. Selection calls ApplyFontSize().

Style Toggles

ButtonShortcutAction
Bold (B)Ctrl+BDocumentController.ApplyBold()
Italic (I)Ctrl+IDocumentController.ApplyItalic()
Underline (U)Ctrl+UDocumentController.ApplyUnderline()
Strikethrough (S)DocumentController.ApplyStrikethrough()
Subscript (X₂)DocumentController.ApplySubscript()
Superscript (X²)DocumentController.ApplySuperscript()

Color

ControlAction
Text Color buttonOpens color picker → DocumentController.ApplyFontColor(color)
Highlight Color buttonOpens color picker → DocumentController.ApplyBackgroundColor(color)

Paragraph Alignment

ButtonAction
Align LeftApplyAlignment(TextAlignment.Left)
Align CenterApplyAlignment(TextAlignment.Center)
Align RightApplyAlignment(TextAlignment.Right)
JustifyApplyAlignment(TextAlignment.Justified)

Lists & Indent

ButtonAction
Bullet ListDocumentController.ToggleBulletList()
Numbered ListDocumentController.ToggleNumberedList()
Indent MoreDocumentController.AdjustParagraphIndent(+20)
Indent LessDocumentController.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.

MethodDescription
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.