Font Family does not change to embedded fonts in XAML components. WPF + .NET.Core 6 App.

Volk Volk 571 Reputation points
2022-12-17T10:02:35.167+00:00

Hi!

I have a WPF+ .NET.Core 6 Visual Studio 2022 application and I need to embed custom fonts into it. I found an example of how to do this, implemented it, but the fonts do not change either in realtime in the editor or in run.

Include Fonts in WPF Application
include-fonts-in-wpf-application

What am I doing wrong?

Maybe I need to change WpfApp4 to something else?
What is it WpfApp4?

Thanks!

App.xaml

<Application ...>  
  
    <Application.Resources>  
          
        <ResourceDictionary>              
            <ResourceDictionary.MergedDictionaries>  
                <ResourceDictionary Source="Fonts.xaml" />  
            </ResourceDictionary.MergedDictionaries>              
        </ResourceDictionary>  
          
    </Application.Resources>  
  
</Application>  
  

Fonts.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">  

    <FontFamily x:Key="BuxtonSketch">pack://application:,,,/WpfApp4;component/Resources/Fonts/#BuxtonSketch</FontFamily>  
    <FontFamily x:Key="Murder">pack://application:,,,/WpfApp4;component/Resources/Fonts/#a-theme-for-murder</FontFamily>  
  
</ResourceDictionary>  

MainWindow.xaml

<Grid>  
          
        <TextBlock Name="VersionText" VerticalAlignment="Bottom" HorizontalAlignment="Right" Text="0.0.0" FontFamily="{StaticResource BuxtonSketch}" FontSize="20" Foreground="White" Margin="0, 0, 5, 0"/>  
  
        <Button Name="PlayButton" Content="Check" Click="PlayButton_Click" Height="60" FontFamily="{StaticResource Murder}" FontSize="28" Foreground="#DDFFFFFF" Margin="0, 0, 0, 15" VerticalAlignment="Bottom" HorizontalAlignment="Center" MinWidth="150" Background="#FFFF9700" Padding="10,1,10,1"/>  
  
    </Grid>  
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,784 questions
0 comments No comments
{count} votes

Accepted answer
  1. Hui Liu-MSFT 48,571 Reputation points Microsoft Vendor
    2022-12-19T08:14:00.73+00:00

    You could modify WpfApp4 to your project name (namespace), my example here should be changed to FontFamilyDemo.

    You should also modify the path of Fonts.xaml according to the path where your fonts are located.

    Project and resource path structure:

    271935-image.png

    2.Right click on the .ttf file, select Properties and set Build Action to Resource:

    271976-image.png

    Fonts.xaml:

     <FontFamily x:Key="BuxtonSketch">pack://application:,,,/FontFamilyDemo;component/Fonts/#SourceSansPro-Bold</FontFamily>  
        <FontFamily x:Key="Murder">pack://application:,,,/FontFamilyDemo;component/Fonts/#Source Sans Pro Black</FontFamily>  
        <FontFamily x:Key="Pacifico">pack://application:,,,/FontFamilyDemo;component/Fonts/#Pacifico</FontFamily>  
    

    MainWindow.xaml:

    <StackPanel>  
            <TextBlock Name="tb" VerticalAlignment="Bottom"  
                       HorizontalAlignment="Right" Text="0.0.0" Background="AliceBlue"   
                       FontSize="20"   
                        Margin="0, 0, 5, 0"/>  
            <TextBlock Name="light" VerticalAlignment="Bottom"  
                       HorizontalAlignment="Right" Text="0.0.0" Background="AliceBlue"   
                       FontFamily="{StaticResource Pacifico}" FontSize="20"   
                        Margin="0, 0, 5, 0"/>  
            <Button Name="btn" Content="Check"  Height="60"   
                    FontSize="28" Foreground="#DDFFFFFF" Margin="0, 0, 0, 15" VerticalAlignment="Bottom"  
                    HorizontalAlignment="Center" MinWidth="150" Background="#FFFF9700" Padding="10,1,10,1"/>  
            <Button Name="PlayButton" Content="Check"  Height="60" FontFamily="{StaticResource Murder}"  
                    FontSize="28" Foreground="#DDFFFFFF" Margin="0, 0, 0, 15" VerticalAlignment="Bottom"  
                    HorizontalAlignment="Center" MinWidth="150" Background="#FFFF9700" Padding="10,1,10,1"/>  
    
        </StackPanel>  
    

    The result:
    271945-image.png


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.