0

I would like to import custom fonts on my WPF application so that they work without having the client to install them.

All the answers I have found so far are in XAML, I would like to do it only in C#.

My fonts are in Resources/Fonts/.

I have already tried this :

 Fonts.GetFontFamilies(new Uri("pack://application:,,,/Resources/Fonts/#"));

But it didn't work.

2 Answers 2

2

I did everything bluetoothfx said but it still did not work.

Then I changed the Build action of my fonts (it was to Content), to Embedded Resource, and it worked. Resource works also for me.

Thanks anyway.

Sign up to request clarification or add additional context in comments.

Comments

0

I think the way that you are working will not work.

At first create a folder name fonts then Add the font to your project, change its Build Action to Content.

Now you need to find the internal name (Real name) of the font not the font-file name. You can have it by opening the font file and you can see it on top.

Now edit App.xaml

<Application.resources>
<style x:key="MYFONT_INTERNAL_NAME">
    <setter property="TextElement.FontFamily" 
        value="pack://application:,,,/fonts/#MYFONT_INTERNAL_NAME" />
</style>

Now use it in your code like:

<TextBlock Style="{StaticResource MYFONT_INTERNAL_NAME}" FontSize="16" Text="Font Style" />

To know more search here: http://www.alteridem.net/2014/02/24/custom-fonts-in-wpf-applications/

4 Comments

If I have multiple fonts, can I just add several <style> values ? Furthermore, how can I access this style property from my C# code ?
Make a settings menu from where you are able to choose font names I mean, you let your user to choose fonts and try to Bind font name Binding{FONT_NAME_PROPERTY} on ViewModel. Then pass the font name. Hope this concept helps.
I have put this in my App.xaml: <Application.Resources> <Style x:Key="Lato Light"> <Setter Property="TextElement.FontFamily" Value="pack://application:,,,/fonts/#Lato Light"/> </Style> </Application.Resources>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.