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?
One simple way to do it programmatically:
Actually you can get a full XAML solution combining some of the other answers here.
If your main window is called
WinMain
(the one you load before all others), just add a reference to a style named e.g.WinAll
and then define your style this way
Try this simple workaround in the
App.xaml
(no code behind needed):Just bind your windows style to this. Works perfectly for me. Only some properties need to be defined in the nested tree. For example the property
FontSize
can be specify only in the generic section.I don't know why is necessary to do this trick. It's strange because Label should be derived from Control. Anyone have any idea about it?
Most of proposed solutions didn't work for me. My simple solution:
Add this to App.xaml:
Add this in your MainWindow constructor (after InitializeComponent):
I found this :
Assuming your
Window
subclasses don't overrideDefaultStyleKey
, you can simply add it to your Window style, sinceTextElement.FontFamilyProperty
is an inherited property:You also need to add the following to your App constructor after the
InitializeComponent
call:How it works: After the App object finishes initializing, the Window style specified therein is made the default style for all windows.