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.

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

Parent

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.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

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

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

"Won't have much of an effect" (1 comment)
"Won't have much of an effect"
Peter Taylor‭ wrote over 2 years ago

Setting it on the Window works fine: all of the controls in the window inherit its FontFamily as long as you set it early enough.