Introduction

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

Background

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 < >.

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)

I've been looking at this on codeplex, I've implemented it into my app and I've noticed that on a mouse hover of the html it changes the html text to red, I was also wondering if it was possible to change the mouse to the hand? then back to the arrow when the mouse is not on the html.

I've tried to figure out how to do this with your sourcecode on codeplex but its above my level of knowledge figuring out where to do it.

I downloaded the sample project with NuGet, and it works great. Everything is exactly like in screenshots you posted. However, when I click on a link, nothing happens. How can I handle this click event?

Very useful control! However, I did run into a slight problem. I'm using this with RSS, and it is not uncommon to have an anchor tag that just contains an image. In that circumstance, the updater tries to put a null value into the Hyperlink, which crashes at runtime.

Hi.I've extended your component to include a few additional options:[s] - strikethrough[font] - can have parameters: name, size and color. color can be a hex string or a name.I've also modified the attribute parsing mechanism to support both quoted and nonquoted values

I hope to implement something like the following : e.g. <a href="{link}"><binding path="header" /></a>Both link and header should able to return value like WPF.

I haven't implement this yet, but I think it will require : - parse the new syntax (sure!)- get the initial value (using reflection i guess)- hook to datacontext's PropertyChanged event, and update the html when fired.

so the link would be databound to some source.in that case I'd use the same code as in xaml:<a href="{Binding Path=URL}">{Binding Path=UrlName}</a>a <binding> element, as in your example, would also be useful this way you could catch the Binding part in the parse method and simply load it with b = new Binding(path)then attach it to the tag's "link" property with this.SetBinding(HtmlTag.LinkProperty, new Binding(linkPath));

I believe if you use FlowDocument (correct me if I am wrong), you can write xaml code like this : "<hyperlink>A Link</hyperlink>"it works, however, you cannot change the content at runtime, unless you write a number of lines in code-behind, e.g : "new Hyperlink(new Run("A Link"));"

Thats what my textblock does, and all you need is to bind the html property.