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?

superjos
  • 12,189
  • 6
  • 89
  • 134
John Batdorf
  • 2,502
  • 8
  • 35
  • 43

4 Answers4

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.

joshperry
  • 41,167
  • 16
  • 88
  • 103
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.

joshperry
  • 41,167
  • 16
  • 88
  • 103
  • 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 '16 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!

sdd
  • 721
  • 9
  • 23
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.

Alexandru Dicu
  • 1,151
  • 1
  • 16
  • 24