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.
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.
2 answers
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.
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.
0 comment threads