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
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...
#1: Initial revision
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.