How to set the default font for a WPF application?

RogueX picture RogueX · Jun 30, 2010 · Viewed 87k times · Source

I want to be able to define a font family for my WPF application. Preferably using a resource dictionary as a theme referenced from App.xaml. I've tried creating a Style as follows:

<Style TargetType="{x:Type Control}">
    <Setter Property="FontFamily" Value="Segoe UI" />            
</Style>

But this doesn't work. Setting the type to TextBlock works for most controls but there are a few controls where this doesn't apply.

I know you can set the font on a window and have all child controls of that window inherit the font. But I think any dialog windows will go back to the default font, which is not exactly what I want.

Any ideas?

Answer

Ray Burns picture Ray Burns · Jun 30, 2010

Assuming your Window subclasses don't override DefaultStyleKey, you can simply add it to your Window style, since TextElement.FontFamilyProperty is an inherited property:

<Style TargetType="{x:Type Window}"> 
    <Setter Property="FontFamily" Value="Segoe UI" />             
</Style> 

You also need to add the following to your App constructor after the InitializeComponent call:

FrameworkElement.StyleProperty.OverrideMetadata(typeof(Window), new FrameworkPropertyMetadata
{
    DefaultValue = FindResource(typeof(Window))
});

How it works: After the App object finishes initializing, the Window style specified therein is made the default style for all windows.