In WPF, how do I reference a font in a resource library in code behind? In WPF, how do I reference a font in a resource library in code behind? wpf wpf

In WPF, how do I reference a font in a resource library in code behind?


I have it working in my application (loading fonts from another assembly in code-behind). For a font URI like this:

pack://application:,,,/MyAssembly.Name;component/Resources/Fonts/#Swis721 Md BT

The way I got it to work (after painful trial and error, if I remember correctly) is:

new FontFamily(    new Uri("pack://application:,,,/MyAssembly.Name;component/Resources/Fonts/"),    "./#Swis721 Md BT")

Hope that helps.


WPF does not support creating the FontFamily object programmatically using pack notation.

The docs say it in the end of the page, here

Here is the quote:

Absolute URI using the pack: notation: WPF applications do not allow you to create a FontFamily object programmatically using "pack:" as part of the absolute uniform resource identifier (URI) reference to a font. For example, "pack://application:,,,/resources/#Pericles Light" is an invalid font reference.


(I know, old question, but I didn't find a correct answer.)

the Ross's answer only works in some versions of the netframework . (Does not work on netframework 4.6)

I think this is the best answer :

Enumerating Fonts in an Application :

 foreach (FontFamily fontFamily in Fonts.GetFontFamilies(new Uri("pack://application:,,,/"), "./resources/"))            {                // Perform action.            }

reference