Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XAML TextBlock set special characters programmatically?

I want to use font awesome (http://fortawesome.github.io/Font-Awesome/design.html) in XAML.

I have been able to easily get it to work via direct XAML, by creating a fonts folder and adding the font there, then in XAML:

<TextBlock FontFamily="Fonts/#FontAwesome">&#xf000;</TextBlock>

Displays a martini glass icon.

However, when adding it programmatically it just shows and invalid symbol like so: [], I tried the following:

XAML:

<TextBlock Name="textBlock"></TextBlock>

C#:

textBlock.FontFamily = new FontFamily("Fonts/#FontAwesome");
textBlock.Text = HttpUtility.HtmlDecode("&#xf000;");

and the following which returns the literal string:

textBlock.FontFamily = new FontFamily("Fonts/#FontAwesome");
textBlock.Text = "&#xf000;";

Any ideas?

like image 927
Andrew Avatar asked May 06 '13 23:05

Andrew


People also ask

How to use special characters in XAML?

How to: Use Special Characters in XAML Character Syntax Description < < Less than symbol. > > Greater than sign. & & Ampersand symbol. " " Double quote symbol. 1 more rows ...

What is the textblock in XAML?

Provides a lightweight control for displaying small amounts of text. For more info, design guidance, and code examples, see Text block. If you have the XAML Controls Gallery app installed, click here to open the app and see the TextBlock in action.

How do I add extended characters to text in WPF?

Text in WPF is always stored as Unicode. Adding extended characters (ones that are not on your keyboard) is easily done in XAML. Just learn this special Unicode syntax . For example, what if you need to add a few Greek characters to your text (π λ)?

How do I use special characters in text when creating markup?

Ampersand symbol. Double quote symbol. Single quote symbol. If you create a markup file using a text editor, such as Windows Notepad, you must save the file in the Unicode UTF-8 file format in order to preserve any encoded special characters. The following example shows how you can use special characters in text when creating markup. <!--


1 Answers

try the following :

textBlock.FontFamily= new FontFamily(new Uri("pack://application:,,,/"), @"/Fonts/#FontAwesome"); // you should well reference your font else you will get a square
textBlock.Text = "\uf000";// \u (unicode escape char) instead of &#x

and if you want to preview your textblock XAML use

t_out.Text = XamlWriter.Save(textBlock);
like image 159
seddik Avatar answered Oct 16 '22 21:10

seddik