MVVM and data binding

    Bind an HTML property when the application owns the content and save state. Choose body content or a complete document as the main binding boundary.

    Application-owned HTML reopened in the Avalonia editor after a real edit and save.
    Application-owned HTML reopened in the Avalonia editor after a real edit and save.

    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.

    Keep document ownership explicit

    Accepted bound properties and the visible source draft are separate state. Resolve a dirty draft before replacing the document from a view model. Save prompts, persistence and navigation belong to the application; browser recreation does not authorize replaying an operation captured from an earlier session.

    Last updated on Oct 18, 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.