COOKIE
COOKIE

Reputation: 394

Unable to import font into WPF application

I'm trying to add a custom font to my application, but I'm getting an error:

An object of the type "System.Windows.Style" cannot be applied to a property that expects the type "System.Windows.Media.FontFamily".

From the error, I'm guessing that it means, the font wasn't imported, but I'm not sure why it wasn't. Here's the code in the MainWindow.xaml file:

<Window.Resources>
    <Style x:Key="Raleway">
        <Setter Property="TextElement.FontFamily" Value="fonts/#Raleway"/>
    </Style>
</Window.Resources>
<TextBlock Text="text" 
          FontWeight="Light"
          FontFamily="{StaticResource Raleway}"
          FontSize="22"
          Foreground="White"
          HorizontalAlignment="Center"
          />

This is my directory:

This is my directory

Upvotes: 1

Views: 453

Answers (1)

Victor
Victor

Reputation: 8925

The following markup shows how to reference the application's font resources:

<Window.Resources>
   <FontFamily x:Key="Raleway">./fonts/#Raleway</FontFamily>
</Window.Resources>

<TextBlock Text="text" 
          FontWeight="Light"
          FontFamily="{StaticResource Raleway}"
          FontSize="22"
          Foreground="White"
          HorizontalAlignment="Center"
          />

When you add fonts as resources to your application, make sure you are setting the <Resource> element, and not the <EmbeddedResource> element in your application's project file. The <EmbeddedResource> element for the build action is not supported.

It is possible to package fonts with WPF application. The detailed information is here: Packaging Fonts with Applications

Upvotes: 2

Related Questions