Reputation: 25
I need to display special characters in TextBlock from string in code.
<TextBlock x:Name="tboxData" TextWrapping="Wrap" Text=">> <<" />
is working, but I need to do it from c#:
tboxData.Text = ">> <<";
And this doesn't print >> << in TextBlock.
How can I do it?
Upvotes: 0
Views: 1588
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(">> <<")
Note that you'll have to add a reference to the System.Web
assembly.
Upvotes: 3
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
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