Monday, August 2, 2010

Wpf Custom Fonts

Easy.

Include the *.ttf file into your WPF project as a Resource.

Reference it into a ResourceDictionary as follows:
<UserControl.Resources>
    <FontFamily x:Key="WeirdFontFamily">/TestProject;Component/Fonts/#Weird</FontFamily>
    <FontFamily x:Key="WeirdBoldFontFamily">/TestProject;Component/Fonts/#Weird Bold</FontFamily>
</UserControl.Resources>
I don't fully understand exactly what the # is doing, but suffice to say in this case it is followed by the font name not the file name.  The font name can be found by double clicking the TTF file which opens it in the WIndows Font Viewer.

Here's a usage example:

<TextBlock 
          FontFamily="{DynamicResource WeirdFontFamily}"
          FontSize="18"
          />

No comments:

Post a Comment