开发者

How to set FontFamily in code

I'm creating a WPF application wher开发者_C百科e 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?


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;


First, give your element a name.

<Run x:Name="someNameHere">ABCDEFGHIJKLMNOPQRSTUVWXYZ</Run>

and now in the code behind you can do set it like in the following examples:

someNameHere.FontFamily = new FontFamily("Monospace");

or

someNameHere.FontFamily = new FontFamily("Comic Sans MS");

or

someNameHere.FontFamily = new FontFamily("Times New Roman");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜