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
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

75%
+4 −0
Q&A How to set FontFamily for an entire WPF application in a theme?

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...

2 answers  ·  posted 2y ago by Peter Taylor‭  ·  last activity 2y ago by aresavatar‭

Question fonts wpf theme
#2: Nominated for promotion by user avatar Alexei‭ · 2022-04-17T07:54:20Z (about 2 years ago)
#1: Initial revision by user avatar Peter Taylor‭ · 2022-04-05T16:13:00Z (about 2 years ago)
How to set FontFamily for an entire WPF application in a theme?
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.