Isuru Herath
Isuru Herath

Reputation: 306

Import a new font into WPF application MVVM

I have file 'resource.xaml' for all the styles. But I need to add a new font file to my project and re-use it instead of default font. How can I add a new font and use it? I searched the internet and see some methods. but nothing worked for me.

Thanks in Advance.

Upvotes: 0

Views: 894

Answers (2)

SamTh3D3v
SamTh3D3v

Reputation: 9944

when using a custom font you must respect the following syntax:

"/FontPath/FontFileName.ttf#FontName"

for exemple :

<Setter Property="FontFamily" Value="/Fonts/VLADIMIR.TTF#Vladimir Script"/>

where the name of this font is "Vladimir Script".

Upvotes: 2

Mike Eason
Mike Eason

Reputation: 9723

I've had this problem before, and here's how I did it:

  1. Create a folder in your solution and add all of the font .ttf files in there.
  2. In App.xaml, or a resource dictionary, add the following code:

    pack://application:,,,/Path/To/Font/#Name Of Font

Then, you can reference the font resource in your XAML code:

<TextBlock FontFamily="{DynamicResource NameOfResource}" Text="Hello World"/>

Upvotes: 0

Related Questions