Setting the Default Font Name and Size at Startup

One of the most frequent WPF support questions on this control is a variant of "FontName, FontSize etc. not working from MVVM" — usually because the customer is binding to a property name that does not exist on the editor, or rebinding on every PropertyChanged tick. This page gives the supported pattern, the XAML equivalent, and the MVVM-specific pitfalls.

Why a dedicated default-font setting exists

An HTML document has no global "font". Appearance is decided by inline styles, <font> tags, CSS rules, or inheritance from a parent. Loading a fragment such as <p>Type here</p> falls back to the WebView/IE host default (typically Times New Roman 12pt). Customers want a deterministic starting point — for example Calibri 12pt.

The cleanest pattern (verified property names)

The WpfHtmlEditor exposes three properties for this purpose directly on the control itself — they are not on the Options object:

  • DefaultFontFamilystring. Writes the supplied value into the body element's inline style="font-family:...".
  • DefaultFontSizeInPtstring. Pass a value with the unit, e.g. "12pt". If you pass a bare number such as "15", the editor treats it as pixels and converts it to the equivalent pt value before showing it in the toolbar, which is rarely what you want.
  • DefaultForeColorSystem.Windows.Media.Color. Writes the body element's inline style="color:...".

Set them in the code-behind constructor (or via XAML attribute) after InitializeComponent() and before any binding fires. The toolbar Font/Size dropdowns are refreshed automatically when the setter runs.

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        // Pre-configure the editor BEFORE any binding writes to BodyHtml.
        MyEditor.DefaultFontFamily   = "Calibri";
        MyEditor.DefaultFontSizeInPt = "12pt";       // always include the unit
        MyEditor.DefaultForeColor    = System.Windows.Media.Colors.Black;
    }
}

Equivalent XAML:

<wpfeditor:WpfHtmlEditor x:Name="MyEditor"
                         DefaultFontFamily="Calibri"
                         DefaultFontSizeInPt="12pt"
                         DefaultForeColor="Black" />

MVVM hint: bind once, do not rebind on every PropertyChanged

The recurring "FontName not working MVVM" ticket usually traces to either binding to a name that does not exist on the editor (e.g. FontName instead of DefaultFontFamily), or raising PropertyChanged on the font properties on every keystroke, which re-applies the body rule mid-typing. Bind once at construction:

<wpfeditor:WpfHtmlEditor x:Name="MyEditor"
                         DefaultFontFamily="{Binding StandardFontName, Mode=OneTime}"
                         DefaultFontSizeInPt="{Binding StandardFontSize, Mode=OneTime}"
                         BodyHtml="{Binding HtmlContent, Mode=TwoWay, UpdateSourceTrigger=LostFocus}" />

Use Mode=OneTime for the font properties. BodyHtml should be the only TwoWay binding, with UpdateSourceTrigger=LostFocus — never PropertyChanged.

Why this differs from assigning BodyHtml with inline styles

A frequent workaround is to seed the editor with markup like <span style="font-family:Calibri;font-size:12pt"></span>. That decorates the already-typed text only. The moment the user clears the document or presses Enter outside the span, the formatting is gone. The three Default* properties write directly to the <body> element's inline style, so the rule is inherited by every paragraph and survives a full clear.

Toolbar Font / Size dropdowns and saved HTML

Each setter writes the value back into FactoryCmbFontName and FactoryCmbFontSize via the Dispatcher, so the dropdowns display "Calibri" and "12pt" the moment the window opens. Because the rule lives on the <body> element, calling MyEditor.BodyHtml or DocumentHtml returns self-describing markup:

<body style="font-family:Calibri; font-size:12pt; color:#000000">
    <p>...your content...</p>
</body>

What happens when the user pastes content from Word

Word puts its own font-family and font-size on every paragraph it copies. Inline styles win over the body-level default (CSS specificity), so the pasted region keeps Word's font. Leave MyEditor.Options.AutoDetectWordPaste on (default in 9.0.x) so the Word-paste cleaner strips those redundant inline styles. Text typed after the pasted block returns to the body default.

Worked example: Calibri 12pt that survives a full clear

MainWindow.xaml — the three defaults are set declaratively:

<wpfeditor:WpfHtmlEditor x:Name="MyEditor"
                         DefaultFontFamily="Calibri"
                         DefaultFontSizeInPt="12pt"
                         DefaultForeColor="Black" />

MainWindow.xaml.cs:

public MainWindow()
{
    InitializeComponent();
    MyEditor.Options.AutoDetectWordPaste = true;
}

private void BtnReset_Click(object sender, RoutedEventArgs e)
{
    // Even after a full clear, the <body> rule survives,
    // so the next character is still Calibri 12pt.
    MyEditor.BodyHtml = string.Empty;
}

The three properties are the single supported entry point for default body typography. Set them at construction (XAML attribute or constructor), bind Mode=OneTime from MVVM, and the "FontName not working MVVM" ticket disappears.

Last updated on May 14, 2026