WPF C# Setting Font Family of Text Block
I am looking for the correct sytanx for setting a custom defined font in the xaml.cs file. In the .xaml file, I can successfully do this:
<TextBlock FontFamily="Resources/#Charlemagne Std" FontSize="22" ... />
However, I cannot seem to get the same thing to work in the .xaml.cs file. I have tried something to this effect:
TextBlock tb = new TextBlock();
tb.FontFamily = new FontFamily(new Uri("pack://application:,,,/"), "#Charlemagne Std");
The font is in the Resources folder of my project folder. The name of the font is Charlemagne Std, and the name of the font file is chalemagnestd-regular.otf.
There were no compile errors, but the font showing does not seem to be correct.
Th开发者_JAVA技巧anks for all the help
If the font is in a subfolder of the project folder, you'll need to include that folder path when constructing the FontFamily object. Use:
new FontFamily(new Uri("pack://application:,,,/"), "./Resources/#Charlemagne Std");
For more information, see Packaging Fonts with Applications on MSDN.
精彩评论