How to display simple HTML in a Silverlight textblock?
Asked Answered
B

3

7

I've got a source of data that has HTML tags in it (B, I, A) and need to display this in a Silverlight ListBox.

Searching around it seems to be an issue but most of the posts are old and have to do with Silverlight 1 it seems.

What is currently the best way to display simple HTML with Silverlight, if nothing else, just the B, I and A tags for bold, italic and hyperlinks?

Bonucci answered 14/4, 2009 at 15:7 Comment(0)
D
3

There's no native support in Silverlight 2.0. However, someone has gone and created a HtmlTextBlock control which should be suitable for your purposes. Check out the author's block post on the subject. There's also a demo page here.

Donell answered 14/4, 2009 at 15:19 Comment(1)
Seems like the server hosting the blog is down at the moment. You can still download the source from delay.members.winisp.net/SilverlightHtmlTextBlock/…. Note that it was written for the Silverlight 2.0 beta, so it may need some minor modifications to work now.Donell
S
0

If you want to do it just in XAML:

<TextBlock>
    Text:&#160;<Italic>italic</Italic>&#160;and&#160;<Bold>bold</Bold>
</TextBlock>

the &#160 is an antity for space. The result is:

Text: italic and bold

Sheol answered 7/9, 2012 at 12:25 Comment(0)
C
-1

This comment system is wacky. I added 2 comments last night. After adding the second one, I could still only see the first one. This morning, after clearing browser cookies, I only see my second one. Wierd.

Anyway, I had a problem with the control where I had its Html property bound, and it was appending html every time the property changed. I fixed it by adding the following to the top of the ParseAndSetText() method:

this.SelectAll();            
this.Selection.Text = "";

I also had a problem where an exception was being thrown when the DOM parsing routine failed, and I fixed it by changing:

Xaml = null;

with

this.SelectAll();            
this.Selection.Text = ""; 
Cotto answered 27/11, 2010 at 8:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.