MVVM and data binding

    Bind the Avalonia HTML editor to a ViewModel with two-way BodyHtml or DocumentHtml binding. Use an awaited export in your Save command to capture current edits, including changes made in Source view.

    Edited HTML reopened in Avalonia HTML Editor with its approval sentence, headings and table preserved.
    Edited HTML reopened in Avalonia HTML Editor with its approval sentence, headings and table preserved.

    Avalonia 12 enables compiled bindings by default. Declare xmlns:vm="clr-namespace:YourApp.ViewModels" on the containing window and set x:DataType to your view-model type. That type exposes a public, writable string Html property and raises PropertyChanged when the value changes.

    <editor:AvaloniaHtmlEditor
        x:DataType="vm:DocumentViewModel"
        BodyHtml="{Binding Html, Mode=TwoWay}" />
    

    HtmlChanged reports accepted content changes. SelectionChanged can refresh application commands. Binding updates and browser communication are asynchronous; save through an awaited GetBodyHtmlAsync or GetDocumentHtmlAsync.

    Protect a source draft

    An external binding replacement cannot silently discard a dirty or pending source draft. Apply the user's draft through the mode/export workflow, or use CancelSourceEdits() after pending work settles if the user chooses to discard it. A rejected binding restores accepted content properties while retaining the source draft, including after browser recreation.

    Document ownership

    Keep save prompts and persistence in the view model. An accepted browser snapshot is immutable, and its document/selection identity belongs to that browser session. Do not reuse a captured operation after switching documents or recreating the native surface.

    Related guides

    Last updated on Oct 30, 2026

    Put this into practice.

    Start with the C# Avalonia sample. Restore this edition's package and install the native browser prerequisites before running it.

    Prefer a guided look? Book a free live demo with our engineers - live on Zoom or Teams, never a chatbot.