87

I'm creating a WPF app and I would like to know the best way to be able to change the font size for every element in the ui. Do I create a resource dictionary and set Styles to set the font size for all the controls I use?

What is the best practice?

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Jose
  • 10,891
  • 19
  • 67
  • 89

11 Answers11

111

I'd do it this way:

<Window.Resources>
        <Style TargetType="{x:Type Control}" x:Key="baseStyle">
            <Setter Property="FontSize" Value="100" />
        </Style>
        <Style TargetType="{x:Type Button}" BasedOn="{StaticResource baseStyle}"></Style>
        <Style TargetType="{x:Type Label}" BasedOn="{StaticResource baseStyle}"></Style>
        <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource baseStyle}"></Style>
        <Style TargetType="{x:Type ListView}" BasedOn="{StaticResource baseStyle}"></Style>
        <!-- ComboBox, RadioButton, CheckBox, etc... -->
    </Window.Resources>

That way, if I want to change ALL the controls, I'd just have to change the "baseStyle" style, the rest would just inherit from it. (That's what BasedOn property those, you can also extend the base style if you create other setters inside of the inherited style)

Carlo
  • 25,602
  • 32
  • 128
  • 176
  • 3
    And if you need to change the font size on the fly, make sure that all of them are declared to DynamicResource, both in the Styles and in the Style references to "baseStyle". – Jeff Wain May 21 '09 at 19:39
  • Seroiouslly? I tried doing that and I get this error: Property 'BaseOn' does not support values of type 'DynamicResourceExtension'. I just changed the code from StaticResource to DynamicResource. What do you think is missing? – Carlo May 21 '09 at 20:05
  • 1
    Forgot about that little issue. We have all our declarations set up as Setters declared per-control so that we don't have huge hierarchies. This post explains it pretty well. http://stackoverflow.com/questions/585429/using-basedon-style-property-on-dynamicresources – Jeff Wain May 21 '09 at 21:32
  • I got a little curious about this issue and I went ahead to do some tests, but it seems like once you've set and are using a StaticResource, you can't modify it. Maybe a different solution is needed if you want to change the size at some point in your app. – Carlo May 21 '09 at 21:59
  • i don't think simply changing the font (face/size) of an item will change its size. – Ian Boyd Aug 21 '11 at 03:53
  • @Ian Boyd: What do you mean? Please elaborate a bit more. – Carlo Aug 21 '11 at 07:09
  • @Carlo Imagine the form is designed with Tahoma 8pt (i.e. Windows XP). Someone runs it on Vista (default font Segoe UI 9pt). If i set the form to Segoe UI 9pt, i don't believe elements will get 16% larger. If running on a computer set to Georgia 14pt, i don't think elements will be 127% larger. Content may expand to fit, but that doesn't solve the problem of items (i.e. buttons) that were all hard-coded to 75 logical pixels wide by 23 logical pixels high. – Ian Boyd Aug 21 '11 at 13:12
  • 2
    @Ian Boyd: But the guy just want to change the font size in all his controls, not the size of the controls. What you're talking about is a whole different issue. – Carlo Aug 21 '11 at 21:40
  • An style's TargetType & its BasedOn style's TargetType SHOULD be the same! So your sample XAML will produce InvalidOperationException. For example when declaring a style with TargetType of TextBlock and using BasedOn pointing to a style with TargetType of Control, the result error will be: InvalidOperationException: Can only base on a Style with target type that is base type 'TextBlock' – Amir Mahdi Nassiri Aug 19 '17 at 11:40
  • The inheritance doesn't follow down both logical and visual trees, does it? this is not a full answer if that is the case. – Maslow Apr 01 '19 at 17:20
50

FontSizeProperty is inherited from Parent Control. So you just need to change FontSize of your main window.

If you don't need dynamic behaviour this should work:

Add a style for Window to your ResourceDictionary

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

Apply the style to your main form (will not be applied implicit because its a derived type)

 Style = (Style)FindResource(typeof (Window));
Matze
  • 1,402
  • 2
  • 17
  • 24
44

<Window> has a property FontSize.

So you can set desired fontsize in element if you want to change the fontsize in all the elements within that window.

<Window FontSize="12">

</Window>
Janaka R Rajapaksha
  • 3,585
  • 1
  • 25
  • 28
  • Applying it to Window via a Setter didn't work (it looked like it did in the designer view though...) but this one did and is a ridiculously simple solution to the problem. – Jonas Dec 19 '18 at 09:33
  • Its Great! it works in about 95% of the app. (excluding ribbon buttons and maybe some few other controls) – Yonatan Tuchinsky Aug 12 '19 at 09:40
  • @YonatanTuchinsky: When it doesn't work, it's probably because a provider with a higher priority determines the dependency property value (e.g. the provider for OS theme, #9 in [this list of precedence](https://learn.microsoft.com/en-us/dotnet/framework/wpf/advanced/dependency-property-value-precedence) while inheritance is only number #10). – mins Nov 02 '19 at 12:09
34

Another option is to define the FontFamily and FontSize as resources.

<FontFamily x:Key="BaseFontFamily">Calibri</FontFamily>
<sys:Double x:Key="BaseFontSize">12</sys:Double>

That way you can use them in your setters.

Neal
  • 349
  • 3
  • 2
  • This works best for me as it allows you to use {DynamicResource } and change the font sizes at runtime. – Peter Aug 05 '14 at 13:07
  • 7
    Had to import _xmlns:sys="clr-namespace:System;assembly=mscorlib"_ and this method worked perfect. – dynamiclynk Aug 05 '14 at 19:52
12
Application.Current.MainWindow.FontSize = _appBodyFontSize;

This way you can change the Font Size at run time also.

Vipul
  • 1,563
  • 4
  • 22
  • 46
7

TextElement.FontSize is an inherit property, which means you can simply set the font size at root element, and all the children elements will use that size (as long as you don't change them manually)

axel22
  • 32,045
  • 9
  • 125
  • 137
Lin
  • 1,016
  • 1
  • 9
  • 3
  • Important: "as long as you don't change them manually". It took me some time to figure out that the font inheritance did work because I set it somewhere for a parent manually. – Peter Huber Dec 30 '14 at 08:45
  • This should be the selected answer. @PeterHuber: Dependency properties are meant to be *dependent* on multiple value providers which have some predetermined priorities. Setting a local value has the highest priority (except animation and coercion, [precedence rules](https://learn.microsoft.com/en-us/dotnet/framework/wpf/advanced/dependency-property-value-precedence)). – mins Nov 02 '19 at 12:17
6

For any styles in WPF, you should have a separate resource dictionary that contains the styles for your app.

If you want to have a single Font Size that's reused throughout the app then just create a style for that font size. You can either give it a unique name/key to use explicitly or you can set a targetType that will transcend throughout the app.

Explicit Key:

<Style
    x:Key="MyFontSize"
    TargetType="TextBlock">
    <Setter
        Property="FontSize"
        Value="10" />
</Style>

<Control
    Style="{StaticResource MyFontSize}" />

*Note this style can be used with controls that have contentPresenters

For all textblocks in the app:

<Style
    TargetType="TextBlock">
    <Setter
        Property="FontSize"
        Value="10" />
</Style>

<TextBlock
        Text="This text will be size 10" />
Chris Nicol
  • 10,256
  • 7
  • 39
  • 49
  • 1
    "*If you want to have a single Font Size that's reused throughout the app then just create a style*": this is in contradiction with the purpose of dependency property `FontSizeProperty`. [Dependency properties are inheritable by principle](https://docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/dependency-properties-overview#property-value-inheritance) in the logical tree, so unless there is a specific need (e.g. having a different font size on an intermediary node only), setting the font size on the root element is enough. Updating the value will trigger an update on all children. – mins Nov 02 '19 at 12:35
3

If you need to programmatically change global FontSize, not statically (XAML), to be applied once for all your windows, you can do:

TextElement.FontSizeProperty.OverrideMetadata(
            typeof(TextElement),
            new FrameworkPropertyMetadata(16.0));

        TextBlock.FontSizeProperty.OverrideMetadata(
            typeof(TextBlock),
            new FrameworkPropertyMetadata(16.0));

This values are applied to any TextBlock, Labels and almost any text in any windows, whereas it has not a explicit FontSize defined. But this does not affect for TextBox, you have to write a similar code for it or any other special controls.

Pablonete
  • 1,504
  • 1
  • 14
  • 11
  • This is much better than a ` – bytecode77 Mar 13 '20 at 09:28
2

To dynamically change the font size globally with ctrl-mousewheel:

XAML:

<Window Name="MainWindow" ... PreviewMouseWheel="MainWindow_PreviewMouseWheel">

code behind:

private void MainWindow_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
    if ((Keyboard.Modifiers & ModifierKeys.Control) != 0)
    {
        if (e.Delta > 0)
            ++mainCtrl.FontSize;
        if (e.Delta < 0 && mainCtrl.FontSize > 1)
             --mainCtrl.FontSize;
    }
}
Jim Balter
  • 16,163
  • 3
  • 43
  • 66
1

Using Resources in XAML is the way to go. Although there are many great answers to this question, I would like to add my two cents to the SCOPE of the Resource.

For Global accessibility in all of the Windows and User Controls of the Project, you can have your resource in the App.xaml file

<Application.Resources>
    <Style TargetType="{x:Type Control}" x:Key="GlobalFontSize">
        <Setter Property="FontSize" Value="28"/>
    </Style>
</Application.Resources>

For accessibility at a Window level, you can have your resource in your xaml file for Window

<Window.Resources>
    <Style TargetType="{x:Type Control}" x:Key="GlobalFontSize">
        <Setter Property="FontSize" Value="28"/>
    </Style>
</Window.Resources>

You could even have it at a Control level, for example

<DockPanel.Resources>
    <Style TargetType="{x:Type Control}" x:Key="GlobalFontSize">
        <Setter Property="FontSize" Value="28"/>
    </Style>
</DockPanel.Resources>
Muhammad Ali
  • 277
  • 3
  • 11
1

Let's have some BLACK MAGIC things:

  1. Add a double resource into your Application resource
<Application.Resources>
    <sys:Double xmlns:sys="clr-namespace:System;assembly=mscorlib" x:Key="GlobalFontSize">12</sys:Double>
</Application.Resources>
  1. Add a static property in your App class
public static double GlobalFontSize
{
    get => (double)Current.Resources["GlobalFontSize"];
    set => Current.Resources["GlobalFontSize"] = value;
}
  1. Use this resource any where you want by DynamicResource
FontSize="{DynamicResource GlobalFontSize}"
  1. Access property App.GlobalFontSize in any way to change value, binding is okay!
App.GlobalFontSize = 20;
//Or
{Binding Path=(local:App.GlobalFontSize)}