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

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 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.