0
votes

I'm new to WPF Interoperability in VB.net Window Forms.

I'm making a Windows Form Application where I'm trying to add custom usercontrols with ElementHost that uses a custom font family.

I've created a custom button with a label inside it, where I have applied a custom font 'Raleway' from Google Fonts that I've loaded into Resources.

<UserControl x:Class="ButtonDark"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:prjButtonTestFont"
         mc:Ignorable="d" 
         d:DesignHeight="50" d:DesignWidth="150">

<Button Content="Button" BorderBrush="{x:Null}" Foreground="{x:Null}" >
    <Button.Template>
        <ControlTemplate TargetType="{x:Type Button}">
            <Border x:Name="Border" Background="#ff332d2f" CornerRadius="4" BorderBrush="#ff4e4749" BorderThickness="4">
                <Label x:Name="Labelx" Content="Click" FontFamily="/prjButtonTestFont;component/Resources/#Raleway Thin" HorizontalAlignment="Center" VerticalAlignment="Center"  Foreground="#ff4e4749" FontSize="24" />
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="IsMouseOver" Value="true">
                    <Setter TargetName="Border" Property="BorderBrush"  Value="green"/>
                    <Setter TargetName="Labelx" Property="Foreground" Value="green"/>
                </Trigger>
                <Trigger Property="IsPressed" Value="true">
                    <Setter TargetName="Border" Property="BorderBrush"  Value="white"/>
                    <Setter TargetName="Labelx" Property="Foreground" Value="white"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Button.Template>
</Button>

Image of xaml design preview showing the font corretly enter image description here

Image of Windows Form with the usercontrol button showing the incorrect font enter image description here

The problem is that the font wont display when I run the application, but it will display correctly in the xaml design preview window.

I've tried all the following file paths to the font, which all show the correct font in the xaml design preview window, but do not when I run the application.

FontFamily="./#Raleway Thin"

FontFamily="/prjButtonTestFont;component/Resources/#Raleway Thin"

FontFamily="../Resources/#Raleway Thin"

FontFamily="../#Raleway Thin"

FontFamily="Raleway-Thin.ttf/#Raleway Thin"

FontFamily="../Resources/Raleway-Thin.ttf/#Raleway Thin"

FontFamily="/prjButtonTestFont;component/Resources/Raleway-Thin.tff/#Raleway Thin"

FontFamily="/Resources/Raleway-Thin.ttf#Raleway Thin"

FontFamily="Raleway-Thin.ttf#Raleway Thin"

FontFamily="Raleway-Thin.TTF#Raleway Thin"

FontFamily="/Resources/Raleway-Thin.TTF#Raleway Thin"

I've also tried applying FontFamily to and with the same results!

I am looking for any solution whether programmatically in vb code or through xaml. I just need the font to show up once the application is launched.

2

2 Answers

0
votes

The way I display fonts is I reference the absolute path of the font. Whenever you use resources located in a library (versus an application), you almost always want the absolute path:

FontFamily="pack://application:,,,/YourAssemblyName;component/Fonts/#Actual Font Name"

Edit: It's worth mentioning that none of the samples you have provided reflect a valid (absolute) path. /prjButtonTestFont;component/Resources/#Raleway Thin comes pretty close, but isn't quite correct. In addition, I would double check the font name is valid. To do this, just open the actual font file and make sure the name at the very top of the document is the same in your project.

0
votes

This question has also answered my question Reference custom view in other project with custom font

The problem was the 'Build Action' was not set to 'Resource'. To do this, select the font in 'Resources' folder and in the 'Properties' window set 'Build Action' to 'Resource'.

Now the font displays when the program is ran.