Welcome to MSDN Blogs Sign in | Join | Help

A fix for simple HTML display in Silverlight [HtmlTextBlock bug fix for Silverlight 2 RTW!]

I updated my HtmlTextBlock sample for RTW last night and got an email from kind reader Ed Silverton this morning pointing out a problem setting FontSize on a standalone instance of the control. (Background reading: HtmlTextBlock Announcement for the Alpha, Improvements, Beta 1 Update, Data Binding Support, Beta 2 Update, RTW Update) Unfortunately, this problem does not demonstrate itself in the sample project, so I missed it. :( Sorry about that!

Ed's scenario was that of setting the FontSize or Foreground properties in the XAML for an instance of HtmlTextBlock; he observed that they did not take effect. What I think happened is that these Silverlight properties became inheritable between Silverlight Beta 2 and RTW, so the code in HtmlTextBlock to set up TemplateBindings to them was interfering with their normal operation. I simply removed the code in HtmlTextBlock that deals with the inheritable properties: FontFamily, FontSize, FontStretch, FontStyle, FontWeight, and Foreground. (Note that TextDecorations doesn't count because it's not present on the Control class HtmlTextBlock derives from.) After that, all was well.

Mostly, that is... Ed's scenario now worked fine and so did the sample page - except for when the font size was changed to a small value. After that, changes to the font or size had no effect. Specifically, this problem seems to occur once the size of the text is small enough that the TextBlock fits completely within the bounds of the HtmlTextBlock. Not completely surprisingly, any changes to the TextBlock.Text property cause it to update itself correctly. I don't have a great deal more time to investigate this at the moment, and it's seeming like it may be an issue with Silverlight's TextBlock (I'll follow up internally), so I worked around the problem in the sample page (note: not the HtmlTextBlock code which I think is correct) by resetting the Text property when the font or size is changed by the user.

HtmlTextBlock Demonstration

I've updated the HtmlTextBlock demonstration page and the source code download, so you can try things out in your browser and/or download the code to see how it works!

Sigh, I knew yesterday's painless migration of this sample went too smoothly to be true... :)

Published Wednesday, November 12, 2008 12:15 PM by Delay
Filed under:

Comments

# Delay's Blog : A fix for simple HTML display in Silverlight [HtmlTextBlock bug fix for Silverlight 2 RTW!]

# Silverlight Cream for November 12, 2008 -- #426

Thursday, November 13, 2008 1:16 AM by Community Blogs

In this issue: Silverlight Girl, Mehdi Slaoui Andaloussi, David Anson, Cheryl, Justin Angel, Kathy Kam

# re: A fix for simple HTML display in Silverlight [HtmlTextBlock bug fix for Silverlight 2 RTW!]

Friday, November 14, 2008 11:42 AM by davidjjon77

Thank you very much for sharing this control David.

One of the serious issues with Silverlight is SEO.  It seems to me that one way of effectively handling SEO is to have an invisible web site of html that only the search bots see - Silverlight could use a control like HtmlTextBlock to read and display appropriate portions of each invisible html page.  Anyway, that's what I am implementing.

I would really support a production level version of HtmlTextBlock becoming a permanent part of the Silverlight Toolkit.

David Roh

JK@SilverlightAzure.com

# hello, please help me!

Wednesday, March 25, 2009 11:04 PM by kangta198588

Thank you for your control,it's very good.I have a quesstion,can you turn the "htmltextblock" to a "htmltextbox"?

Because I need  the text to be selected by the cursor.The "textblock" doesn't support it.

# re: A fix for simple HTML display in Silverlight [HtmlTextBlock bug fix for Silverlight 2 RTW!]

Thursday, March 26, 2009 2:36 AM by Delay

kangta198588,

I'm afraid this transition would probably be quite a bit of work. That's not to say it's not possible to write a RichTextBox for Silverlight (search the web - others have done it!), but just to say that I don't see an easy path from my HtmlTextBlock sample to an HtmlTextBox.

Hope this helps!

# re: A fix for simple HTML display in Silverlight [HtmlTextBlock bug fix for Silverlight 2 RTW!]

Thursday, March 26, 2009 3:21 AM by kangta198588

Delay,

thank you for reply so quickly.I have already downloaded the RichTextBox,but it doesn't support to translate html.Actually,it's not necessary for me to change an HtmlTextBlock to an HtmlTextBox ,I just want to select the text by the cursor in an HtmlTextBlock.Can it  be achieved ?

# re: A fix for simple HTML display in Silverlight [HtmlTextBlock bug fix for Silverlight 2 RTW!]

Thursday, March 26, 2009 5:53 PM by Delay

kangta198588,

Not easily, I'm afraid...

# re: A fix for simple HTML display in Silverlight [HtmlTextBlock bug fix for Silverlight 2 RTW!]

Friday, July 03, 2009 9:00 AM by antonmaju

Hi Delay,

The source code link seems to be broken. Could you reupload the source code?

Thanks

# re: A fix for simple HTML display in Silverlight [HtmlTextBlock bug fix for Silverlight 2 RTW!]

Saturday, July 04, 2009 1:39 AM by Delay

antonmaju,

Sorry for the trouble! My web site hoster seems to be having issues - I've contacted them now to report the problem and ask about a resolution. Please check back a few times over the next few days as I hope to have the issue resolved fairly quickly.

If it's urgent, please use the "Email" link at the upper-right of the page to contact me and I'll send you the files in email for now. :)

Thanks!

# re: A fix for simple HTML display in Silverlight [HtmlTextBlock bug fix for Silverlight 2 RTW!]

Thursday, July 30, 2009 3:46 AM by jongalloway

What's the license for this code? MsPl? And... any thoughts about putting it up at CodePlex or MSDN Code Gallery?

# re: A fix for simple HTML display in Silverlight [HtmlTextBlock bug fix for Silverlight 2 RTW!]

Friday, July 31, 2009 4:33 PM by Delay

Jon,

Pretty much everything on my blog is Ms-PL (I've got a link to that effect in the sidebar, but I agree it's not super-obvious). In particular, HtmlTextBlock *is* Ms-PL and you're welcome to use it however you'd like! :)

# re: A fix for simple HTML display in Silverlight [HtmlTextBlock bug fix for Silverlight 2 RTW!]

Friday, July 31, 2009 4:35 PM by Delay

Jon,

I forgot to address the second part of your comment. :( Yes, I occasionally consider putting my stuff up on some public code site, but whenever I do, the overhead of that process just seems excessive for the benefit folks will get out of it. If you have particular opinions on the matter, I'd be happy to chat more - just contact me via the "Email" link in the sidebar.

Thanks for your help!

Anonymous comments are disabled
 
Page view tracker