Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set FontFamily in code

Tags:

wpf

I'm creating a WPF application where I need to use custom fonts. A created a font resource library as described here http://msdn.microsoft.com/en-us/library/ms753303.aspx. An example shows how to set a font family in XAML:

<Run FontFamily="/FontLibrary;Component/#Kootenay" FontSize="36">
  ABCDEFGHIJKLMNOPQRSTUVWXYZ
</Run>

How do I set a font family in the code?

like image 709
DSK Avatar asked Jul 13 '11 16:07

DSK


1 Answers

Assign a Name to your run and then construct the FontFamily with the URI constructor:

Xaml:

<Run x:Name="MyTextRun">ABC</Run>

Code behind:

MyTextRun.FontFamily = new FontFamily(new Uri("/FontLibrary;Component/#Kootenay", UriKind.RelativeOrAbsolute), "Kootenay");
MyTextRun.FontSize = 36;
like image 90
Ed Bayiates Avatar answered Sep 28 '22 01:09

Ed Bayiates