Version

Changing Default Classification Types Appearance (xamSyntaxEditor)

Topic Overview

Purpose

This topic provides information on how to change the colors and other appearance attributes assigned to language elements by the xamSyntaxEditor™ .

Required background

The following topics are prerequisites to understanding this topic:

Topic Purpose

In this topic, you will find information to help you better understand the xamSyntaxEditor’s functions.

This topic covers the text editing capabilities of the xamSyntaxEditor control from both the developer and user’s perspective.

This topic lists the languages supported by the xamSyntaxEditor and shows you how to use each of them.

Introduction

Customizing predefined languages using classification appearance mapping

The xamSyntaxEditor supports several predefined languages each of which define a specific grammar to classify and colorize discrete parts of the document’s text.

After lexing the TextDocument, an internal ClassificationTagger service begins to assign a ClassificationType to each text part. ClassificationTypes represent basic language element types such as "Keyword", "Operator" and "Literal".

TextDocumentAppearance is an object that exposes properties such as Foreground, FontItalic and FontBold.

A ClassificationAppearanceMap associates ClassificationTypes with TextDocumentAppearance instances. The xamSyntaxEditor exposes an instance of this map via its ClassificationAppearanceMap property. The default ClassificationAppearanceMap`,` provided by the xamSyntaxEditor assigns colors to ClassificationTypes that are similar to the default colors assigned by Visual Studio.

The xamSyntaxEditor uses the ClassificationTypes assigned to each language element along with the ClassificationAppearanceMap to colorize each language element as it is rendering on the display.

In some cases, you may need to change the predefined colorization and styling. To do this, you can modify the default ClassificationAppearanceMap provided by the xamSyntaxEditor to add new mappings or remove existing mappings. Alternatively, you can create a new ClassificationAppearanceMap instance with the desired appearances and mappings and assign it to the xamSyntaxEditor.ClassificationAppearanceMap property. In this scenario, if your custom map does not contain an entry for a particular ClassificationType, the xamSyntaxEditor will fall back to using the default ClassificationAppearanceMap for that ClassificationType.

Note
Note

Mapping entries added in the ClassificationApperanceMap property have a higher priority than the styling and colorization defined by the highlighting languages.

Argument types Details

ClassificationType

The classification type specifies the language part you want to change (for example, comments, keywords, operators, string literals…).

TextDocumentAppearance

This class holds the values used to change various aspects of the text styling (for example, color, font family, font style…).

Add ClassificationAppearanceMap Entry in Code

Overview

Add a new classification appearance map entry to modify the highlighting set by the current language.

Property settings

In order to: Use this method: And specify:

Add a new classification appearance map entry

  • ClassificationType

  • TextDocumentAppearance

Example

In C#:

// create a new classification appearance map
ClassificationAppearanceMap classMap =
    new ClassificationAppearanceMap();
// create a new text document appearance object
TextDocumentAppearance tda = new TextDocumentAppearance();
// change the foreground color only
tda.Foreground = new SolidColorBrush(Colors.Red);
// add a new entry in the classification map
// for the "keyword" classification type
// the last argument is true if you want to
// replace existing entry for the same
// classification type
classMap.AddMapEntry(
    ClassificationType.Keyword,
    tda,
    true
);
// assign the classification map to the xamSyntaxEditor
this.xamSyntaxEditor1.ClassificationAppearanceMap = classMap;

In Visual Basic:

' create a new classification appearance map
Dim classMap As New ClassificationAppearanceMap()
' create a new text document appearance object
Dim tda As New TextDocumentAppearance()
' change the foreground color only
tda.Foreground = New SolidColorBrush(Colors.Red)
' add a new entry in the classification map
' for the "keyword" classification type
' the last argument is True if you want to
' replace existing entry for the same
' classification type
classMap.AddMapEntry(ClassificationType.Keyword, tda, True)
' assign the classification map to the xamSyntaxEditor
Me.xamSyntaxEditor1.ClassificationAppearanceMap = classMap

Add ClassificationAppearanceMap Entry in XAML

Overview

The control supports defining new classification appearance map entries in XAML. The code snippet below demonstrates how to add new classification appearance map entries which change the language rendering:

  • keywords’ uses a red foreground and bold styling

  • strings’ uses a magenta foreground and italic styling

Example

Following is the code that implements this example.

In XAML:

<Page.Resources>
    <ResourceDictionary>
        <ig:ClassificationAppearanceMap x:Key="CustomClassificationMap">
            <ig:ClassificationAppearanceMapInitializationEntry
                Classification="Keyword">
                <ig:ClassificationAppearanceMapInitializationEntry.Appearance>
                    <ig:TextDocumentAppearance FontBold="True">
                        <ig:TextDocumentAppearance.Foreground>
                            <SolidColorBrush Color="Red" />
                        </ig:TextDocumentAppearance.Foreground>
                    </ig:TextDocumentAppearance>
                </ig:ClassificationAppearanceMapInitializationEntry.Appearance>
            </ig:ClassificationAppearanceMapInitializationEntry>
            <ig:ClassificationAppearanceMapInitializationEntry
                 Classification="StringLiteral">
                 <ig:ClassificationAppearanceMapInitializationEntry.Appearance>
                     <ig:TextDocumentAppearance FontItalic="True">
                        <ig:TextDocumentAppearance.Foreground>
                            <SolidColorBrush Color="Magenta" />
                        </ig:TextDocumentAppearance.Foreground>
                    </ig:TextDocumentAppearance>
                </ig:ClassificationAppearanceMapInitializationEntry.Appearance>
            </ig:ClassificationAppearanceMapInitializationEntry>
        </ig:ClassificationAppearanceMap>
    </ResourceDictionary>
</Page.Resources>
<ig:XamSyntaxEditor
    ClassificationAppearanceMap="{StaticResource CustomClassificationMap}"
/>

Remove ClassificationAppearanceMap Entry

Overview

Delete a classification appearance map entry to remove a custom defined highlighting.

Property settings

In order to: Use this method: And specify:

Remove a classification appearance map entry

  • ClassificationType

Example

In C#:

classMap.RemoveMapEntry(ClassificationType.Keyword);

In Visual Basic:

classMap.RemoveMapEntry(ClassificationType.Keyword)

Related Topics

The following topics provide additional information related to this topic.

Topic Purpose

This topic provides information about text colorization, based on the TextDocument’s associated language..

The topics in this group explain the ways to customize the xamSyntaxEditor .