![]() |
Platforms, Frameworks & Libraries »
Windows Presentation Foundation »
Controls
Beginner
License: The GNU Lesser General Public License
WPF Html supported TextBlockBy Leung Yat ChunThis Article describes HtmlTextBlock, which is a WPF TextBlock that can parse limited set of html tags and display them. |
C#, Windows, WPF, Architect, Dev, Design
|
||||||||||
|
Advanced Search Add to IE Search |
|
|
|

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.
<Window x:Class="HtmlTextBlockTestProj.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:uc="http://www.quickzip.org/UserControls"
Title="HtmlTextBlockTest" Height="250" Width="450">
<DockPanel>
<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"
VerticalScrollBarVisibility="Visible"
x:Name="tb"
Text="The [i][u]quick brown fox[/i][/u] jumps over the [b]lazy dog[/b]" />
</DockPanel>
</Window>
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 that08-02-09 Initial version
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 7 Feb 2009 Editor: |
Copyright 2009 by Leung Yat Chun Everything else Copyright © CodeProject, 1999-2009 Web10 | Advertise on the Code Project |