Both Visual Studio 2008 and 2010 have a rather annoying issue relating to how XAML is highlighted in the Text Editor. I’ve complained in the past that the XAML designer team introduced a separate set of classifications (i.e. options in the Fonts and Colors dialog) to configure how XAML code would be highlighted by the editor, when in fact, it is just, for the most part XML and it would’ve been used if the XAML editor just used the same color definitions used by the XML editor.

However, this isn’t 100% true. It turns out that the XAML code editor uses the colors and font settings configured for String (e.g. like a C# string) to render raw text inside an XML element in the XAML code. Normally, this isn’t too much of a problem, until you create a color scheme that uses a custom background color to highlight strings. Then, this is what you end up with in XAML:


Notice how the different background is used to render even spaces used for indentation, which looks very, very ugly.

Now, there’s nothing I can do about VS2008, but at least with VS2010, I was able to write an extension that fixes this problem. Here’s how it looks with the extension enabled:


Much better, right?

Here’s basically all the code needed to fix this:

[Name("XAML Fix")]
public class XamlFixCreationListener : IWpfTextViewCreationListener {
IClassificationTypeRegistryService registry = null;
IClassificationFormatMapService mapService = null;

public void TextViewCreated(IWpfTextView textView) {
var formatMap = mapService.GetClassificationFormatMap(textView);
var textClassification = registry.GetClassificationType("text");
var stringClassification = registry.GetClassificationType("string");
var props = formatMap.GetExplicitTextProperties(textClassification);
formatMap.SetExplicitTextProperties(stringClassification, props);

As usual, you can get the code for the extension from github. Enjoy!

Tomas Restrepo

Software developer located in Colombia.