As the name of the article 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.
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.
In my implementation (See source code) - there are two pieces of development:
- 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.
- 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.
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">
In MainWindow.xaml.cs, add the
SpellingErrorColorizer to the line transformers.
Note: In this implementation, I did not care about the performance. If you have a better suggestions, please share/contribute.
- 03/11/13 - First spell check idea.