Click here to Skip to main content
11,928,078 members (37,908 online)
Click here to Skip to main content
Add your own
alternative version


25 bookmarked

BBCode Parser for WPF

, 27 Nov 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
A customizable BBCode parser for WPF


This article is about a custom WPF control that enables the definition and usage of BBCode in your XAML application.


I was trying to create an ASP.NET backoffice for basic CMS operations on a Silverlight page. Since XAML/XML is hard to edit without some kind of code completion, I decided to give BBCode a shot, since it's easy and the square brackets look a lot nicer to the user. Smile | :)

Parser Design


The design of the control is fairly simple. It has a BBCode dependency property that stores the BBCode currently on display, a collection of Tags that are used during the parsing, a DefaultElementName that is used for elements without any tag, and a RootElementName, that is used as a root for the parsed XAML output.

The Parse method utilizes the tags to transform the BBCode into XAML and then uses the XamlReader.Load method to set the UserControl.Content property of the Parser control. 

Explanation of the Tag Objects

  • SimpleTag: [;)]
  • EnclosingTag: [i]text[/i]
  • ValueTag: [url][/url]
  • ParameterValueTag: [url=][url]
  • ParametrizedTag: [img width=100 height=200]image.jpg[/img]

You can add your own tags if you wish. Just implement the ITag interface, and you can use it in Parser.Tags in code or XAML! 

Using the Code

Here is a example of how one can define custom BBCodes for the Parser control:

BBCode definition - Click to enlarge

And this is the demo app: 


Since the BBCode and Tags are dependency properties, you can use bindings on them. In the demo app, I have bound the Textbox.Text to the Parser.BBCode property. And you have a (very) simple editor!

Points of Interest  

My first attempt to write this control was with regular expressions, but I decided to write the parsing manually because I had problems with nested BBCodes. 

I had problems with special Slovenian characters (š, c, ž) using the XamlReader.Parse(string) method. I had to use... 

XamlReader.Load(new MemoryStream(Encoding.UTF8.GetBytes(xaml)), pc);  

... to get it to work properly. Seems like the Parse method uses Encoding.ASCI to get the bytes from the input string.


  • 27-11-2008: First release


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer
Slovenia Slovenia
m0sa is a .NET Software Developer, currently working at Stack Exchange

Check out my latest pet project
Karate Semaphore

You may also be interested in...

Comments and Discussions

BugUrgent : Displaying custom UserControl in the parser (not CustomControl) Pin
Member 1099113329-Aug-14 23:51
memberMember 1099113329-Aug-14 23:51 
GeneralRe: Urgent : Displaying custom UserControl in the parser (not CustomControl) Pin
Member 1099113330-Aug-14 0:57
memberMember 1099113330-Aug-14 0:57 
QuestionLive typing Pin
Member 109911334-Aug-14 10:37
memberMember 109911334-Aug-14 10:37 
AnswerRe: Live typing Pin
Member 109911334-Aug-14 10:54
memberMember 109911334-Aug-14 10:54 
GeneralWorks like a charm Pin
manulg11-Sep-13 8:16
membermanulg11-Sep-13 8:16 
GeneralRe: Works like a charm Pin
m0sa11-Sep-13 12:23
memberm0sa11-Sep-13 12:23 
GeneralLittle bug Pin
ogauchard3-Jul-09 4:31
memberogauchard3-Jul-09 4:31 
GeneralA new implementation Pin
PJonDevelopment4-May-09 17:59
memberPJonDevelopment4-May-09 17:59 
QuestionNested Tags??? Pin
sibercyberia27-Nov-08 21:22
membersibercyberia27-Nov-08 21:22 
AnswerRe: Nested Tags??? Pin
Steve Hansen27-Nov-08 22:24
memberSteve Hansen27-Nov-08 22:24 
GeneralRe: Nested Tags??? Pin
m0sa28-Nov-08 1:27
memberm0sa28-Nov-08 1:27 
GeneralRe: Nested Tags??? Pin
Roberto Collina28-Nov-08 1:36
memberRoberto Collina28-Nov-08 1:36 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151126.1 | Last Updated 27 Nov 2008
Article Copyright 2008 by m0sa
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid