Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the default font for a WPF application?

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?

like image 819
RogueX Avatar asked Jun 29 '10 23:06

RogueX


People also ask

What is the WPF default font?

In WPF, the default font family for text displayed on controls (like Label and Button) is Segoe UI, with a default size of 12.0 device-independent units. Because a device-independent unit is 1/96 inch, a FontSize value of 12 represents characters whose capitals are 1/8″ high.

Where do I put Styles in WPF?

You can use a style on any element that derives from FrameworkElement or FrameworkContentElement such as a Window or a Button. The most common way to declare a style is as a resource in the Resources section in a XAML file.

Does WPF use C#?

WPF, stands for Windows Presentation Foundation is a development framework and a sub-system of . NET Framework. WPF is used to build Windows client applications that run on Windows operating system. WPF uses XAML as its frontend language and C# as its backend languages.


2 Answers

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.

like image 136
Ray Burns Avatar answered Oct 13 '22 06:10

Ray Burns


Most of proposed solutions didn't work for me. My simple solution:

Add this to App.xaml:

<Style TargetType="{x:Type Window}">     <Setter Property="FontSize"             Value="14" /> </Style> 

Add this in your MainWindow constructor (after InitializeComponent):

Style = (Style)FindResource(typeof(Window)); 
like image 41
Jowen Avatar answered Oct 13 '22 06:10

Jowen