Jozo Panacik
Jozo Panacik

Reputation: 25

Set text with special characters

I need to display special characters in TextBlock from string in code.

<TextBlock x:Name="tboxData" TextWrapping="Wrap" Text="&gt;&gt; &lt;&lt;" />

is working, but I need to do it from c#:

tboxData.Text = "&gt;&gt; &lt;&lt;";

And this doesn't print >> << in TextBlock.

How can I do it?

Upvotes: 0

Views: 1588

Answers (3)

mletterle
mletterle

Reputation: 3968

Assuming you might need to use encoded characters for some reason (perhaps you're not using literals and are reading in an xml file or something):

tboxData.Text = System.Web.HttpUtility.HtmlDecode("&gt;&gt; &lt;&lt;")

Note that you'll have to add a reference to the System.Web assembly.

Upvotes: 3

keyboardP
keyboardP

Reputation: 69362

Shouldn't this just work?

tboxData.Text = ">><<";

The reason the XAML requires you to use the encoding is because XAML parses the < and > characters, so you need to ensure that the parser knows that you want to display the <> characters and not parse them as tokens.

Upvotes: 4

e_ne
e_ne

Reputation: 8459

Just use:

tboxData.Text = ">><<";

You don't have to worry about HTML entities when writing a string in C#. It has to be done in XAML because those characters represent opening and closing tags of your code.

Upvotes: 3

Related Questions