Last Updated: February 25, 2016
·
1.682K
· chandramouleswaran

AvalonEdit and SpellCheck

Source code
Download from CodeProject

Introduction
As the name of the tip implies, this is a quick hack to add spell check in your application using the brilliant AvalonEdit. This is a quick tricky/dirty way to implement spell check using the built-in spell checker in .NET without relying on 3rd party libraries. Note that this might not be the most efficient way of spell check.

Background
AvalonEdit is a popular open source text editor control used in #D and widely used by many open source developers in their open source projects. When I started using AvalonEdit, I wanted to add spell check in my application. Initially, I was under the assumption that AvalonEdit inherits from TextBoxBase which means that I can just use the inbuilt spell checker. However that is not the case - AvalonEdit inherits from Control.

The Idea
In my implementation (See source code) - there are two pieces of development:

  1. Every AvalonEdit text editor control has a corresponding TextBox control exclusively to get spelling errors. Every change to the text editor will also update the text box - thereby letting the application get the spelling errors. This has been implemented by creating a behavior called SpellCheckBehavior for AvalonEdit.
  2. A new DocumentColorizingTransformer called the SpellingErrorColorizer, is available as a part of the code. This needs to be added to the list of line transformers in the text view of your editor. The idea behind this transformer is using a static text box which can give the errors on the DocumentLine that needs to be processed.

The Result
The result

Using the code
Download the code and have a look at it as it is self explanatory.
In MainWindow.xaml, create an instance of the text editor and add the behavior.

<avalonedit:TextEditor Name="textEditor" FontSize="20">
    <i:Interaction.Behaviors>
        <SpellCheckAvalonEdit:SpellCheckBehavior />
    </i:Interaction.Behaviors>
</avalonedit:TextEditor>

In MainWindow.xaml.cs, add the SpellingErrorColorizer to the line transformers.

public MainWindow()
{
InitializeComponent();
textEditor.TextArea.TextView.LineTransformers.Add(new SpellingErrorColorizer());
}

Note: In this implementation, I did not care about the performance. If you have a better suggestions, please share/contribute.

My original tip can be found at http://www.codeproject.com/Tips/560292/AvalonEdit-and-Spell-check