Welcome to MSDN Blogs Sign in | Join | Help

Continuing support for simple HTML display in Silverlight [HtmlTextBlock sample updated for Silverlight 2 Beta 1!]

A few months ago when Silverlight 1.1 Alpha was all the rage, I wrote a sample control that made a best-effort attempt to display simple HTML markup in Silverlight. The original HtmlTextBlock post described the control's purpose and the follow-up post detailed a number of improvements to HtmlTextBlock. (Please continue to refer to those posts for background and implementation details.) Now that Silverlight 2 Beta 1 is released, I've had a few internal and external requests to modify HtmlTextBlock to work with the latest Silverlight bits.

HtmlTextBlock Demonstration

I've updated the original HtmlTextBlock demonstration page and also updated the original source code download, so don't hesitate to try things out in your own browser and/or download the code to see how it works!

Notes:

  • There were no fundamental changes to the features HtmlTextBlock uses, so the port from 1.1 Alpha to 2 Beta 1 was straightforward.
  • Thanks to Silverlight 2's flexible layout system, the code to override Width/Height/ActualWidth/ActualHeight and handle the Loaded event is all now unnecessary and has been removed.
  • The XAML passed to Control.InitializeFromXaml now specifies the default XML namespace.
  • Silverlight 2 Beta 1's TextBlock exposes the new properties LineHeight, LineStackingStrategy, and TextAlignment which HtmlTextBlock mirrors.
  • A handful of properties/classes changed names/types slightly.
  • The syntax for creating an instance of the Silverlight control in HTML has changed to use the <OBJECT> tag.

Curiously, certain text/style/size combinations don't seem to render under Silverlight 2 Beta 1. For example, viewing the initial sample text at Verdana/8, Lucida Sans Unicode/10, or Times New Roman/10 shows an empty box. Because changing the font face/size doesn't actually change the Silverlight object tree that HtmlTextBlock creates, I'm inclined to believe this is a rendering issue with the Silverlight Beta rather than a bug in HtmlTextBlock. However, if anyone finds otherwise, please let me know!

It's always great to hear that people have found value in the stuff I've posted to my blog. I hope that those of you who are using HtmlTextBlock continue using it successfully with Silverlight 2 Beta 1 - and that newcomers find something useful as well!

Published Monday, March 17, 2008 12:46 PM by Delay
Filed under:

Comments

# Silverlight Cream for March 17, 2008 -- #227

Monday, March 17, 2008 5:17 PM by Community Blogs

Brad Abrams posted links the the SL2 poster, Shawn Wildermuth has links out to a discussion of a Cross

# Ressources développeur pour Silverlight 2

Monday, March 17, 2008 6:05 PM by Pierre Lagarde, Blog

Depuis la mise a disponibilité de Silverlight 2 au Mix08 il y a eu beaucoup d'articles, de blogs sur

# re: Continuing support for simple HTML display in Silverlight [HtmlTextBlock sample updated for Silverlight 2 Beta 1!]

Monday, March 17, 2008 11:34 PM by eibrahim

Thanks for this control, but it is not working for me.  I tried to add it to a databound listbox and it wouldn't show up.

My listbox item temple looks like this:

<DataTemplate x:Key="ItemReviewTemplate">

           <Grid Height="90" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"

                 Visibility="Visible">

               <StackPanel x:Name="pnlItemInfo" Width="Auto" HorizontalAlignment="Stretch">

                   <TextBlock x:Name="txtSummary2" Text="{Binding Summary}" TextWrapping="Wrap"

                                        HorizontalAlignment="Stretch" MinWidth="200"></TextBlock>

                   <local:HtmlTextBlock x:Name="txtSummary" Text="{Binding Summary}" TextWrapping="Wrap"

                                        HorizontalAlignment="Stretch" MinWidth="200">

                   </local:HtmlTextBlock>

                   <!--<local:HtmlTextBlock x:Name="txtContent" Text="{Binding Content}"

                                        TextWrapping="Wrap" HorizontalAlignment="Stretch"

                                        MinWidth="200" UseDomAsParser="true"></local:HtmlTextBlock>-->

               </StackPanel>

           </Grid>

       </DataTemplate>

# re: Continuing support for simple HTML display in Silverlight [HtmlTextBlock sample updated for Silverlight 2 Beta 1!]

Tuesday, March 18, 2008 2:13 PM by Delay

eibrahim,

This is a consequence of me doing as simple a port as possible. :( Recall that Silverlight 1.1 Alpha did not support data binding, so this issue simply couldn't exist there. Now that Silverlight 2 Beta 1 supports data binding, HtmlTextBlock needs a minor change to support it. In particular, its properties need to be backed by DependencyProperties in order for data binding to work as we'd like. I'll be updating the sample soon to add this support, but for now if you go ahead and replace the Text property definition of the HtmlTextBlock class with the following, then your scenario should work fine:

   public static DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(HtmlTextBlock),

       delegate(DependencyObject o, DependencyPropertyChangedEventArgs e) { ((HtmlTextBlock)o).ParseAndSetText((string)(e.NewValue)); });

   public string Text

   {

       get { return (string)GetValue(TextProperty); }

       set { SetValue(TextProperty, value); }

   }

Please let me know if this doesn't solve the problem!

# Code to support new features doesn't write itself [HtmlTextBlock sample gets data binding support!]

Tuesday, March 18, 2008 4:33 PM by Delay's Blog

In the comments to my post about porting the HtmlTextBlock sample to Silverlight 2 Beta 1 , kind reader

# Again with the support for simple HTML display in Silverlight [HtmlTextBlock sample updated for Silverlight 2 Beta 2!]

Wednesday, June 11, 2008 1:15 PM by Delay's Blog

A customer recently asked about an update to my HtmlTextBlock sample for the newly released Silverlight

Anonymous comments are disabled
 
Page view tracker