WPF - Problem accessing Roboto fonts by font name

1.1k Views Asked by At

I have downloaded the Roboto font from dafunt.com. In my application, I require to use three Roboto-fonts:

  • Roboto Bold
  • Roboto Medium
  • Roboto Regular

But I have trouble identifying how to access the correct font by its font name in my WPF-application. If I open each font-file in Window's Character map application I can see what I think are the font names. These are:

File-name             Font name
===============================
Roboto-Bold.ttf       Roboto
Roboto-Medium.ttf     Roboto Lt
Roboto-Regular.ttf    Roboto

Notice Robot-Bold and Roboto-Regular have the same font name.

So I'm confused about how to access the correct font in my WPF-application.

In my Visual Studio project, I have created a folder called Fonts. The three font files are placed in that folder and the font files are included in my project. The Build Action is set to Resource of each font file.

XAML-code

<Window x:Class="Main.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:Main"
    xmlns:fa="http://schemas.fontawesome.io/icons/"
    mc:Ignorable="d"
    Loaded="Window_Loaded"
    Title="MainWindow" Height="450" Width="800">

    <Window.Resources>
        <Style x:Key="RobotoBold">
            <Setter Property="TextElement.FontFamily" Value="pack://application:,,,/Main;component/Fonts/#Roboto" />
        </Style>
        <Style x:Key="RobotoMedium">
            <Setter Property="TextElement.FontFamily" Value="pack://application:,,,/Main;component/Fonts/#Roboto Lt" />
        </Style>
        <Style x:Key="RobotoRegular">
            <Setter Property="TextElement.FontFamily" Value="pack://application:,,,/Main;component/Fonts/#Roboto" />
        </Style>

        <Style x:Key="RobotoBoldV2">
            <Setter Property="TextElement.FontFamily" Value="pack://application:,,,/Main;component/Fonts/#Roboto Bold" />
        </Style>
        <Style x:Key="RobotoMediumV2">
            <Setter Property="TextElement.FontFamily" Value="pack://application:,,,/Main;component/Fonts/#Roboto Medium" />
        </Style>
        <Style x:Key="RobotoRegularV2">
            <Setter Property="TextElement.FontFamily" Value="pack://application:,,,/Main;component/Fonts/#Roboto Regular" />
        </Style>
    </Window.Resources>

    <StackPanel Orientation="Vertical">
        <TextBlock Text="asdf1" Style="{StaticResource RobotoBold}" />
        <TextBlock Text="asdf2" Style="{StaticResource RobotoMedium}" />
        <TextBlock Text="asdf3" Style="{StaticResource RobotoRegular}" />
        <Separator Height="6" Visibility="Hidden" />
        <TextBlock Text="asdf4" Style="{StaticResource RobotoBoldV2}" />
        <TextBlock Text="asdf5" Style="{StaticResource RobotoMediumV2}" />
        <TextBlock Text="asdf6" Style="{StaticResource RobotoRegularV2}" />
        <Separator Height="6" Visibility="Hidden" />
        <TextBlock Text="asdf7" />
    </StackPanel>
</Window>

I have included an image of the application in the Designer.

enter image description here

Notice there are some variations of the font for some of the TextBlocks. These variations are what I cannot understand.

  • asdf1: Seem to have changed by the font Roboto. If this is Roboto-Bold or Roboto-Regular is unknown.

  • asdf2: Seem to be unchanged.

  • asdf3: Is the same as asdf1 naturally.

  • asdf4: I tried with Roboto Bold but it didn't work.

  • asdf5: For some reason Roboto Medium works. But there is no font file with the name Roboto Medium.

  • asdf6: I tried with Roboto Regular but it didn't work.

  • asdf7: Just for comparison. Seems to be the same font as asdf2, asdf4 and asdf6.

This is so confusing. I'm sure I use the correct syntax to reference the font files in the XAML-code. But since the font names don't seem too reliable I'm not sure which fonts are used in my TextBlocks.

Any suggestion on how to clear this mess?

1

There are 1 best solutions below

0
On

I added another TextBlock, bellow my ASDF1 TextBlock, but with not Font and same Text. I was able to see some difference. So the fonts actually work. Thanks @UuDdLrLrSs.

Still strange that Roboto-Bold and Roboto-Regular has the same font name.