Spellchecking and Suggested Spellings in a TextBox

WPF has built-in support for spellchecking.  Unfortunately the SDK docs on the spellchecking features suck (to put it nicely).  For example, it never even mentions which languages are supported by the spellchecker!

I just spent a few hours trying to come up with a slick way to provide spelling suggestions for misspelled words.  I put the suggestions (which are provided by the spellchecking APIs) into a ListBox and then showed it in a Popup.  After fiddling around trying getting it “just right” I discovered that the functionality already exists.  The docs just never mention it…

The built-in support is not quite as cool as what I was trying to do, but here’s what the provided functionality looks like:
Spellcheck in action

Notice that if you bring up a context menu on a misspelled word, the menu lists suggestions for that word.

One way to enable the spellchecking functionality on a TextBox or RichTextBox is by setting SpellCheck’s IsEnabled attached property to true, like so:

<TextBox SpellCheck.IsEnabled=”True” />

Here’s a snippet of the code I was working on before I made the discovery.  It takes the suggested words and shows them in a ListBox.  This code would be useful if you wanted a portion of the UI to display suggestions, without requiring the user to open a context menu.

if( this.textBox.CaretIndex > 0 )
{
 int idx = this.textBox.CaretIndex – 1;
 SpellingError error = this.textBox.GetSpellingError( idx );
 if( error != null )
  this.listBox.ItemsSource = error.Suggestions;
}

One Response to Spellchecking and Suggested Spellings in a TextBox

  1. KamalV says:

    Hi Josh,
    Great work here on your blog. I was looking around for WPF demos, and ran into your site. It is cool. E-mail me when you find time.