If you are writing code for a while now then by now you must have a lot of code snippets which you will be using in your application development, and you use them because they save a lot of development time. At this moment we have now a huge collection of code snippets which includes functions, classes, extension methods and functions that we have extracted from different open source applications.

As we were progressing towards building an application in WPF which helps us managing all our code, a thought ran into our mind that it would be good if we could use syntax highlighting in the code. As usual we began my search to find a control in WPF which supports syntax highlighting and what we found, We were and we are at present satisfied. The control called AvalonEdit is a part of the free IDE called SharpDevelop for C# and VB.NET and Boo projects.

Languages support by the control:
- ASP/XHTML
- HTML
- JavaScript
- XML
- XMLDoc
- VB.NET
- C#
- C++
- Coco
- Java
- PHP
- Patch
- Boo
- TeX
- Custom Highlighting



After adding the reference in your project, add below XAML code on the window where you have your code window.



At line 4, we will use custom mapping (because it is a third-party control) so we can use the control in our project. At line 7, I have used TextEditor class of the AvalonEdit namespace. The font name and size is the same as of the source code text editor in Visual Studio 2010.

To get the control working with least configuration set some namespaces on the top, and two lines of code on window load for syntax highlighting and to show line numbers respectively.

Namespaces:


Window_Loaded:



If you wish to change the language, then just change the name of the language which is passed as a parameter in the GetDefinition method. The code in the Window_Loaded method will allow you to set syntax highlighting specifically for C#, pretty simple but not very useful. Check out the other way where the text editor will load the file and by reading the file extension, it will set the syntax highlighting. Above method will be useful if the user wants to set syntax highlighting of his choice. But if you want to detect the language and get the syntax highlighting automatically, then use the below code.



 The first line of code will Load the file and the second file will first get the extension of the file loaded, set the instance of the HighlightingManager class and in the end set the syntax highlighting. This is what we got in the end (We are using the second way to load the file). 

 


Note: To make sure that the second method work, you need to make sure that the file should have a language extension like .cs for C#, cpp for C++, xml for XML files etc. AvalonEdit is an open source code, so you can play around with it and can have your own customizations. There are lots of in-built configurations that you can do to set up your syntax highlighting control. I strongly recommend you to download the below files and take a look at the sample application.