Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Notifications
Mark all as read
Q&A

How to set FontFamily for an entire WPF application in a theme?

+4
−0

I have a project Anonymised.Theme which contains XAML resource dictionaries for a theme. This is used by about a dozen applications which simply include it by merging

<ResourceDictionary Source="/Anonymised.Theme;component/DefaultTheme.xaml" />

into <Application.Resources> in their respective App.xaml files.

Now the designer has asked me to use a custom typeface rather than defaulting to Segoe UI. I have added the TTF file to Anonymised.Theme and embedded it appropriately with

  <ItemGroup>
    <Resource Include="Themes\Tajawal-Regular.ttf">
      <CopyToOutputDirectory>Never</CopyToOutputDirectory>
    </Resource>
  </ItemGroup>

The problem is how to actually use it by default without touching anything outside the theme.

The non-solution which "works"

I can add

<FontFamily x:Key="Tajawal">pack://application:,,,/Anonymised.Theme;component/Themes/#Tajawal</FontFamily>

to the theme XAML and then manually set

FontFamily="{StaticResource Tajawal}"

on every single window in every single application which uses the theme, but that's almost completely defeating the point of defining a theme. In particular, it creates a massive maintenance headache, because then I need to remember to do that every single time I create a new dialog in the future.

An almost-solution

Less of a maintenance headache, but still not really a true solution, is to add the <FontFamily> to the theme XAML as before and then make an edit per application to override the default style of Window by adding the following to the App codebehind:

    public App()
    {
        InitializeComponent();

        FrameworkElement.StyleProperty.OverrideMetadata(typeof(Window), new FrameworkPropertyMetadata
        {
            DefaultValue = new Style
            {
                Setters =
                {
                    new Setter(Window.FontFamilyProperty, FindResource("Tajawal")),
                }
            }
        });
    }

Things which don't work at all

Although it feels like it should be the same as the almost-solution, the following XAML:

  <Style TargetType="Window">
    <Setter Property="FontFamily" Value="{StaticResource Tajawal}" />
  </Style>

(in the theme or directly in App.xaml) doesn't appear to be used at all.

Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

2 answers

+2
−0

While writing the question I did find one hacky solution which works, and which I will use unless someone has an alternative.

Create a codebehind file for the theme:

using System;
using System.Windows;
using System.Windows.Media;

namespace Anonymised.Theme
{
    public partial class DefaultTheme : ResourceDictionary
    {
        static DefaultTheme()
        {
            var tajawal = new FontFamily(new Uri("pack://application:,,,/Anonymised.Theme;Component/"), "./Themes/#Tajawal");
            FrameworkElement.StyleProperty.OverrideMetadata(typeof(Window), new FrameworkPropertyMetadata
            {
                DefaultValue = new Style
                {
                    Setters =
                    {
                        new Setter(Window.FontFamilyProperty, tajawal),
                    }
                }
            });
        }
    }
}

Ensure it's used by adding

    x:Class="Anonymised.Theme.DefaultTheme"

to the attributes of the ResourceDictionary in DefaultTheme.xaml.

Then this will be executed while each application is initialising its own resource dictionary, before it creates any windows.

Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+2
−0

Edit: I think you've already rejected this solution, but I am guessing that you only set the property for type Window, which won't have much of an effect. Window controls don't display text except in the titlebar, and you can't affect the titlebar display without completely restyling the window.

The best suggestion I have is to add implicit styles to the resource dictionary for the control types you want this font set on. For example:

<Style TargetType="{x:Type TextBlock}">
    <Setter Property="TextElement.FontFamily" Value="{StaticResource Tajawal}" />
</Style>

<Style TargetType="{x:Type TextBox}">
    <Setter Property="TextElement.FontFamily" Value="{StaticResource Tajawal}" />
</Style>

(etc., set all the common controls here like ComboBox, CheckBox, RadioButton...)

These styles are implicit, meaning they will apply to all controls of the specified type, because there's no x:Key specified.

If you set an explicit style for a control (meaning one with an x:Key), just make sure that you add the same font-family setter to the explicit style, or create a base style which contains it and base the explicit styles on the base style.

Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment thread

"Won't have much of an effect" (1 comment)

Sign up to answer this question »

This community is part of the Codidact network. We have other communities too — take a look!

You can also join us in chat!

Want to advertise this community? Use our templates!

Like what we're doing? Support us! Donate