Set the default font
Default font settings give newly typed content a consistent starting appearance. Set DefaultFontFamily, DefaultFontSizeInPt and DefaultForeColor before users start typing. Existing HTML can carry its own formatting, so these defaults do not restyle every loaded document.
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 IE WebBrowser 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:
DefaultFontFamily-string. Writes the supplied value into the body element's inlinestyle="font-family:...".DefaultFontSizeInPt-string. 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.DefaultForeColor-System.Windows.Media.Color. Writes the body element's inlinestyle="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;
}
}Partial Public Class MainWindow
Inherits Window
Public Sub New()
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
End Sub
End ClassEquivalent 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:
DefaultFontFamily and DefaultFontSizeInPt are plain CLR properties, not dependency properties, so an XAML markup extension such as {Binding ...} cannot target them - WPF only allows a Binding on a DependencyProperty of a DependencyObject, and setting one on either property throws a XamlParseException the moment the window loads. BodyHtml is the only one of the three that is a real dependency property, so it is the only one that belongs on the XAML side of a binding:
<wpfeditor:WpfHtmlEditor x:Name="MyEditor"
BodyHtml="{Binding HtmlContent, Mode=TwoWay, UpdateSourceTrigger=LostFocus}" />Set the two font properties once from code-behind instead, right after InitializeComponent(), pulling the values from the DataContext:
public MainWindow()
{
InitializeComponent();
var vm = (MainViewModel)DataContext;
MyEditor.DefaultFontFamily = vm.StandardFontName;
MyEditor.DefaultFontSizeInPt = vm.StandardFontSize;
}Public Sub New()
InitializeComponent()
Dim vm As MainViewModel = CType(DataContext, MainViewModel)
MyEditor.DefaultFontFamily = vm.StandardFontName
MyEditor.DefaultFontSizeInPt = vm.StandardFontSize
End SubUse a single content binding, typically BodyHtml with TwoWay and LostFocus. Avoid binding both HTML properties to competing model values. Call UpdateBindings() before a save that reads the model while the editor still has focus.
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;
}Public Sub New()
InitializeComponent()
MyEditor.Options.AutoDetectWordPaste = True
End Sub
Private Sub BtnReset_Click(sender As Object, e As RoutedEventArgs)
' Even after a full clear, the <body> rule survives,
' so the next character is still Calibri 12pt.
MyEditor.BodyHtml = String.Empty
End SubThe 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.
Ask your AI to do this
Connect the SpiceLogic MCP server, then give your coding assistant this prompt.
I use the SpiceLogic WPF HTML Editor. Help me set the default font family, size and color before loading the document. Confirm the exact API names, types and defaults with the SpiceLogic MCP documentation, then show the changes for my project.