'WPF - Problem showing a symbol from a custom font

I have a custom font. The font contains symbols at specific addresses. I want to display one of the symbols in a TextBlock.

The font (ttf-file) is located in my applications's folder Fonts. I have included the font in the application and set the BuildAction to Resource.

XAML

I load the symbol from a specific "address" in the font.

But my application is unable to show the symbol. Instead I see a single empty square.

Here is my code. Notice the style and two TextBlocks.

<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"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">

    <Window.Resources>
        <Style x:Key="Foo">
            <Setter Property="TextElement.FontFamily" Value="pack://application:,,,/Main;component/Fonts/#MyFont" />
        </Style>
    </Window.Resources>

    <StackPanel Orientation="Vertical">
        <TextBlock Text="&#xE905;" Style="{StaticResource Foo}" />
        <TextBlock FontFamily="Segoe MDL2 Assets" Text="&#xE777;" />
    </StackPanel>
</Window>
  • I'm sure I load the font correctly in the style.
  • The font's name is correct. (Actually, the font's name is the same as the font's file name).
  • The first TextBlock is where I try to load the symbol from the font.
  • I know that the address E905 is correct.
  • The second TextBlock is just for comparison. With a built-in font and to verify that the syntax to load a symbol at a specific address is correct. (You should be able to copy that line into any WPF-application).

Try in code

I have also tried this with the code-behind.

Adding the Loaded event and added one more TextBlock with a the name txt.

<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"
    mc:Ignorable="d"
    Loaded="Window_Loaded"
    Title="MainWindow" Height="450" Width="800">

<Window.Resources>
    <Style x:Key="Foo">
        <Setter Property="TextElement.FontFamily" Value="pack://application:,,,/Main;component/Fonts/#GuardTools-Symbol-Bold" />
    </Style>
</Window.Resources>

<StackPanel Orientation="Vertical">
    <TextBlock Text="&#xe905;" Style="{StaticResource Foo}" />
    <TextBlock FontFamily="Segoe MDL2 Assets" Text="&#xE777;" />
    <TextBlock x:Name="txt" />
</StackPanel>
private void Window_Loaded(object sender, RoutedEventArgs e)
{
    txt.FontFamily = new FontFamily("pack://application:,,,/Main;component/Fonts/#GuardTools-Symbol-Bold");
    txt.Text = "\xe905";  // &#xe905
}

Same problem...

What could be the problem?

I have also installed the font on my computer, but with no success.



Solution 1:[1]

I am not pretty sure this could be the root cause of the problem but, can you try to use TargetType property in your Style like this:

<Window.Resources><Style x:Key="Foo" TargetType="TextBlock">
        <Setter Property="FontFamily" Value="pack://application:,,,/Main;component/Fonts/#MyFont" />
    </Style></Window.Resources>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 trix