16

I'm trying to add a custom font as a resource in my application. I have a "CustomFont" directory in the application and all the fonts inside of it are set to "Resource"

<Window.Resources>
    <Style x:Key="Gotham-XLight">
        <Setter Property="TextElement.FontFamily" 
                Value="/CustomFonts;Component/#Gotham-XLight" />
    </Style>
</Window.Resources>

And then on my TextBlock I have this: (inside a grid)

<TextBlock x:Name="TimeTextBlock" Style="{DynamicResource Gotham-XLight}" 
           TextAlignment="Center" FontSize="25" FontWeight="Bold" 
           Foreground="White" Text="TextBlockTimer" 
           Margin="105,242.974,0,226.975" HorizontalAlignment="Left" 
           Width="221.919" />

But I'm not seeing my font as people say. Am I doing something wrong?

4 Answers 4

23

You may want to check the name of the font, you need to specify the name of the font not the name of the file.

Double click on the font file and it should show a "Font name:" that's what you want to make sure is specified in your style.

0
7

Try this

<Window.Resources>
    <Style x:Key="Gotham-XLight">
        <Setter Property="TextElement.FontFamily" Value="CustomFonts/#Gotham-XLight" />
    </Style>
</Window.Resources>

Also, if you are not planning on changing the style at runtime {StaticResource Gotham-XLight} will be much more performant.

1
  • I couldn't make it work with {StaticResource StyleName} and had to revert back to {DynamicResource StyleName} after realizing what the problem was.
    – Dinei
    Jan 20, 2016 at 12:13
2

In xaml I did it like this:

    <Button Grid.Column="1" Grid.RowSpan="2" Name="start" Margin="5" Click="start_Click">
        <TextBlock Name="test" FontFamily="pack://application:,,,/Y_Yoga;Component/Resources/#FontAwesome">&#xF04B;</TextBlock>
    </Button>

However, I don't know if #FontAwesome is font's embedded name or is it the result that I renamed the .ttf file.

Hope to be helpful!

3
  • Trying to understand pack syntax: what does the Y_Yoga represent? Jun 6, 2013 at 1:04
  • 2
    It`s just a custom name of an app. So the path should be like FontFamily="pack://application:,,,/<YOUR_APP_NAME_HERE>;Component/Resources/#<YOUR_FONT_NAME_HERE>"
    – sdd
    Jun 11, 2013 at 16:29
  • <YOUR_APP_NAME_HERE> should be <YOUR_ASSEMBLY_NAME_HERE>
    – highboi
    Mar 4, 2021 at 22:08
1

Late reply but worth mentioning. To add a custom font that will apply globally in your window you could add this in your csproj file to include the fonts from the Fonts folder of your project as resources.

 <ItemGroup>
    <Resource Include="Fonts\*.ttf" />
 </ItemGroup>

Then in your window XAML you can specify the FontFamily in the Window part:

<Window x:Class="Namespace.MainWindow"
    ...
    FontFamily="/Fonts/#[FONT NAME]"
    Title="">
  <Grid>
    ...
  </Grid>
</Window>

I hope this could help somebody, as I spent some time to figure it out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.