Skip to main content

Microsoft Silverlight

Answered Question Special charactersRSS Feed

(0)

Galo
Galo

Member

Member

39 points

72 Posts

Special characters

Hi Folks,

 Just wondering if it's posible to display characters like ™ through ™ or something, is it posible ?

thanks!

Foo Bar this

pbrooks
pbrooks

Contributor

Contributor

2671 points

355 Posts

Silverlight MVP
Answered Question

Re: Special characters

Galo,

You can use the following:

<TextBlock Text="Hello World &#8482;"></TextBlock>

 For a more complete list, please see the following resource:

http://www.w3.org/TR/REC-html40/sgml/entities.html

If this has answered your question, please click on "Mark as Answer" on this post.

Thanks,
Page Brooks
Silverlight MVP, MCSD
PageBrooks.com | @pbrooks

surbhiydv
surbhiydv

Participant

Participant

1134 points

218 Posts

Re: Special characters

A few more special characters:

Character

Encoding

<

&lt;

>

&gt;

&

&amp;

&quot;

Space

&#160;

robertlair
robertlair

Member

Member

79 points

43 Posts

Answered Question

Re: Special characters

Also, to add to this.  You can only use this syntax if you are putting the information in the XAML tag itself (also note, you cant use the special HTML characters, you must convert them all to Unicode... so your trademark would be &#8482;. 

If you need to assign the value in code, you will want to use the System.Windows.Browser.HttpUtility.HtmlDecode() method.  So if you want to set the text property of the TextBlock x:Name="Title" to Microsoft Vista™ then you would want to use the following code:

1    Title.Text = System.Windows.Browser.HttpUtility.HtmlDecode("Windows Vista&#8482;");
That will allow you to display the trademark symbol in the TextBlock at runtime. 

Furthermore, if you want to to use it in data binding... You will need to use a value converter within the Binding extension property.  So basically, your XMAL would look like this:

1    <TextBlock Text="{Binding Title, Converter={StaticResource htmlDecodeConverter}}" />
And your source code would look like this:
 
1    public class  HtmlDecodeConverter : IValueConverter
2 {
3 public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
4 {
5 return System.Windows.Browser.HttpUtility.HtmlDecode(value as string);
6 }
7
8 public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
9 {
10 throw new NotImplementedException();
11 }
12 }
PLEASE NOTE: I typed this in, did not copy and paste, so there may be typos.  But hopefully it gives you the right idea.  You will also need to include a static resource tag at the top of your XAML and you will need to add the xmlns to your namespace.  For example, if your namespace is SpecialCharacters you will want to add the xmlns:local="clr-namespace:SpecialCharacters" and then in your UserControl:

<UserControl.Resources>
  <local:HtmlDecodeConverter x:Key="htmlDecodeConverter" />
</UserControl.Resources>

Thats about it!  Hope this answers your question! 

--
Robert Lair
http://blog.robertlair.net
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities