Click here to Skip to main content
13,351,225 members (34,927 online)
Click here to Skip to main content
Add your own
alternative version


34 bookmarked
Posted 7 Feb 2009

WPF Html supported TextBlock

Rate this:
Please Sign up or sign in to vote.
This Article describes HtmlTextBlock, which is a WPF TextBlock that can parse limited set of html tags and display them.
This is an old version of the currently published article.


This Article describes HtmlTextBlock, which is a WPF TextBlock that can parse limited set of html tags and display them.


I was working on a custom progress dialog, which contains a Header, Message, Progress and some action button,
To make it look better, I want the message to support some text format, and I want it to be changeable at runtime, but it seems impossible using TextBlock.


I then googled how to use html in wpf but most solutions told me to use WebBrowser, which is a bit overkill for my purpose. 

Then I remembered an abondoned project I wrote few years ago (mostly because I moved to WPF) , which tried to recreate FlowDocument in dotNet2, and load Html to that flow document (my main purpose, the component was named QzMiniHtml2).

Surprisingly, with very few modifications (mostly using import), this dotNet2 project work nicely with WPF, just as you look above.

Because of this, the original project is also included as well.

How to use? 

The control is similar to TextBlock except you set the Html property instead of Text.
Remember to use [ ] bracket instead of < >.
<Window x:Class="HtmlTextBlockTestProj.Window1"




    Title="HtmlTextBlockTest" Height="250" Width="450">
        <uc:HtmlTextBlock Html="{Binding Text, ElementName=tb}"  

                 DockPanel.Dock="Top" Loaded="HtmlTextBlock_Loaded" />
        <TextBlock Text="[b] [i] [u] [a href=xx] [br] supported." 

                 DockPanel.Dock="Bottom" />
        <TextBox TextWrapping="Wrap" AcceptsReturn="True" 



        Text="The [i][u]quick brown fox[/i][/u] jumps over the [b]lazy dog[/b]" />

How it works? 

The component actually included a html parsing engine inside, which translate html string to WPF's Bold, Italic, Underline, Hyperlink, LineBreak Inline
(more can be added in future, you can do it yourself easily, see below)

The conversion part is simple.

1) private Inline UpdateElement(HtmlTag aTag)
2) {
3)   Inline retVal = null;
4)   switch (aTag.Name)
5)   {
6)     case "text" : 
7)       retVal = new Run(aTag["value"]);			
8)       if (currentState.Bold) retVal = new Bold(retVal);
9)       if (currentState.Italic) retVal = new Italic(retVal);
0)       if (currentState.Underline) retVal = new Underline(retVal);
A)       break;
B)     case "br" : 
C)       retVal = new LineBreak();
D)       break;
E)   }

F)   if (currentState.HyperLink != null && currentState.HyperLink != "")
G)   {
H)     Hyperlink link = new Hyperlink(retVal);
I)     link.NavigateUri = new Uri(currentState.HyperLink);
J)     retVal = link;
K)   }			
L)    return retVal;
M) }		 
First, please note that
  • The input (aTag) is a Text or LineBreak(br), if the tag is a text, Tag["value"] is the text it holds.
    Bold, Italic etc can also be represented by HtmlTag, but they wont be executed here.
  • CurrentState hold the style affecting the TextTag etc
So if the tag is text (line 6),
  • it will generate a Run (which can contain format or unformat text, and unformat in this case) (line 7)
  • if it's bold, italic and underline, it will construct them, and using last Inline (Abstract class, Run, Bold etc inherited from it), so it contain the property.
When the Inline is returned (Line L)
  • It will be added to your HtmlTextBlock.Inlines collection. 


08-02-09 Initial version 


This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


About the Author

Leung Yat Chun (Fainx)
Hong Kong Hong Kong
From the end of the Universe we have Fainx and Quick Zip.
http://www.FANIX.ME is stolen by and is the temporary site.
May Thy Lord Your God be blessed always.

DirectoryInfoEx - [1.0.27]
WPF FileExplorer3 - [3.0.19]
WPF HtmlTextBlock - [Codeplex]
WPF ListView MultiSelect - [0.4]
WPF UIEventHub MultiSelect/DragDrop w Touch support - [3.0]
WPF BreadcrumbFolderTextBox - [2.5]
WPF BreadcrumbTree and Breadcrumb
WPF Aero Titlebar - [0.2]

You may also be interested in...

Comments and Discussions

Discussions on this specific version of this article. Add your comments on how to improve this article here. These comments will not be visible on the final published version of this article.
QuestionChanging mouse on hover? Pin
Andy Cartwright30-Aug-15 10:53
memberAndy Cartwright30-Aug-15 10:53 
QuestionHow to handle click Event Pin
Member 1118186626-Oct-14 17:07
memberMember 1118186626-Oct-14 17:07 
AnswerRe: How to handle click Event Pin
Leung Yat Chun27-Oct-14 0:59
professionalLeung Yat Chun27-Oct-14 0:59 
GeneralRe: How to handle click Event Pin
Member 1168374112-May-15 3:26
memberMember 1168374112-May-15 3:26 
GeneralMy vote of 1 Pin
starsky_chen6-Jan-13 6:16
memberstarsky_chen6-Jan-13 6:16 
QuestionUnable to download the attached sample Pin
ksvimalraj28-Jul-11 18:28
memberksvimalraj28-Jul-11 18:28 
AnswerRe: Unable to download the attached sample Pin
Leung Yat Chun29-Jul-11 4:52
memberLeung Yat Chun29-Jul-11 4:52 
GeneralSmall bug Pin
ArlenFeldman25-Mar-11 15:55
memberArlenFeldman25-Mar-11 15:55 
GeneralThis is ... Pin
Xmen W.K.2-Jul-10 6:25
memberXmen W.K.2-Jul-10 6:25 
GeneralSaving me a lot of time! Pin
Member 324083012-Dec-09 6:01
memberMember 324083012-Dec-09 6:01 
Generalimage in text block Pin
pathurun8-Sep-09 22:35
memberpathurun8-Sep-09 22:35 
GeneralRe: image in text block Pin
Leung Yat Chun15-Sep-09 1:56
memberLeung Yat Chun15-Sep-09 1:56 
GeneralDoes not work if Html Text is assigned to the HtmlTextBlock during Binding Pin
azamsharp1-Jul-09 11:09
memberazamsharp1-Jul-09 11:09 
GeneralRe: Does not work if Html Text is assigned to the HtmlTextBlock during Binding Pin
Leung Yat Chun2-Jul-09 8:06
memberLeung Yat Chun2-Jul-09 8:06 
Generalextensions Pin
Lee_Nover20-Mar-09 16:11
memberLee_Nover20-Mar-09 16:11 
GeneralRe: extensions Pin
Leung Yat Chun20-Mar-09 21:35
memberLeung Yat Chun20-Mar-09 21:35 
GeneralRe: extensions Pin
Lee_Nover23-Mar-09 5:14
memberLee_Nover23-Mar-09 5:14 
GeneralRe: extensions Pin
Leung Yat Chun23-Mar-09 11:20
memberLeung Yat Chun23-Mar-09 11:20 
GeneralRe: extensions Pin
Lee_Nover24-Mar-09 4:45
memberLee_Nover24-Mar-09 4:45 
GeneralRe: extensions Pin
Leung Yat Chun25-Mar-09 10:20
memberLeung Yat Chun25-Mar-09 10:20 
GeneralRe: extensions Pin
Bob Ranck4-Apr-09 10:26
memberBob Ranck4-Apr-09 10:26 
GeneralRe: extensions Pin
Leung Yat Chun5-Apr-09 21:45
memberLeung Yat Chun5-Apr-09 21:45 
QuestionFlow document? Pin
Member 17809897-Feb-09 22:32
memberMember 17809897-Feb-09 22:32 
AnswerRe: Flow document? Pin
Leung Yat Chun7-Feb-09 23:39
memberLeung Yat Chun7-Feb-09 23:39 
GeneralRe: Flow document? Pin
Member 17809898-Feb-09 23:38
memberMember 17809898-Feb-09 23:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180111.1 | Last Updated 7 Feb 2009
Article Copyright 2009 by Leung Yat Chun (Fainx)
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid