WPF has a great feature for spell checking called SpellCheck, which is applied on TextBox and RichTextBox controls. In this post I will explain about it and how can it be customized. Lets go back to the start, to activate the spell checker just set SpellCheck.IsEnabled property to True. Easy, right?. Take a look at the XAML code:

    <TextBox Name="myTextBox" SpellCheck.IsEnabled="True"></TextBox>

Now, when a word is poorly written in the text box, spell check is automatically activated and:

  • Underlines the misspelled word in red.
  • By right clicking this word, a context menu with possible suggestions is opened. After choosing one of the possibilities, the misspelled word will be replaced.

WPF SpellCheck

I must point out that the picture above shows how SpellCheck looks using .Net 3.0 (it looks different using .Net 3.5). Lets talk about the supported languages, WPF come with 4 language dictionaries: English, Spanish, French and German. To change the current dictionary, specify the value of Language property:

    <TextBox Name="myTextBox" SpellCheck.IsEnabled="True" Language="en-us"></TextBox>

The list of spelling suggestions is taken from a copy of the dictionary used by Office2007. Because this is only a copy, you don’t need Office 2007 installed on your computer, everything will work just fine. The first question that came to my mind was, if the dictionary used by WPF can be replaced by another dictionary. Well, the answer is that this feature is not supported in the current WPF version. Too bad…

So, we may want to expand the SpellCheck dictionary programmatically or change the context menu appearance. For that work around, we need to create our own context menu:

private ContextMenu spellCheckContextMenu;
void OnWindowLoaded(object sender, RoutedEventArgs e)
    spellCheckContextMenu = new ContextMenu();
    myTextBox.ContextMenu = spellCheckContextMenu;

The OnWindowLoaded method is called once, so we create a new context menu and set it to the myTextBox.ContextMenu property. We are not done yet because the context menu is empty. To get the default behavior WPF provides , simply add the following code:

   1: void tb_ContextMenuOpening(object sender, RoutedEventArgs e)
   2: {
   3:     // Clear the context menu from its previous suggestions.
   4:     spellCheckContextMenu.Items.Clear();
   6:     // Get the spelling error and add its suggestions to the context menu.
   7:     int caretIndex = myTextBox.CaretIndex;
   8:     int cmdIndex = 0;
   9:     SpellingError spellingError = myTextBox.GetSpellingError(caretIndex);
  10:     if (spellingError != null)
  11:     {
  12:         foreach (string str in spellingError.Suggestions)
  13:         {
  14:             MenuItem mi = new MenuItem();
  15:             mi.Header = str;
  16:             mi.FontWeight = FontWeights.Bold;
  17:             mi.Command = EditingCommands.CorrectSpellingError;
  18:             mi.CommandParameter = str;
  19:             mi.CommandTarget = myTextBox;
  20:             myTextBox.ContextMenu.Items.Insert(cmdIndex, mi);
  21:             cmdIndex++;
  22:         }
  23:         // Add separator lines and IgnoreAll command.
  24:         Separator separatorMenuItem1 = new Separator();
  25:         myTextBox.ContextMenu.Items.Insert(cmdIndex, separatorMenuItem1);
  26:         cmdIndex++;
  27:         MenuItem ignoreAllMI = new MenuItem();
  28:         ignoreAllMI.Header = "Ignore All";
  29:         ignoreAllMI.Command = EditingCommands.IgnoreSpellingError;
  30:         ignoreAllMI.CommandTarget = myTextBox;
  31:         myTextBox.ContextMenu.Items.Insert(cmdIndex, ignoreAllMI);
  32:         cmdIndex++;
  33:         Separator separatorMenuItem2 = new Separator();
  34:         myTextBox.ContextMenu.Items.Insert(cmdIndex, separatorMenuItem2);
  35:     }

The tb_ContextMenuOpening method is called whenever the context menu is about to open. Here we get the spelling error and add its suggestions to the context menu, then add the ‘Ignore All’ command.

Now, we can add our code to add more suggestions (after line 22) and commands (after line 33). Do you want to change the context menu appearance? that is easy too, just implement MyContextMenu which inherit from ContextMenu and set it to myTextBox.ContextMenu property (in the OnWindowLoaded method).


Tags :

14 Responses to “Customize SpellCheck on WPF Text Controls”

  1. martin

    Said on April 2, 2008 :

    Where did the post go?

  2. Shahar Y

    Said on April 2, 2008 :

    The post is right here.

  3. Fernando

    Said on April 28, 2008 :

    Hi, I need your help.

    I can’t enable spellchecking for SPANISH. Only works for english.

    I have .NET 3.5 and VS 2008. Do I need something else?


    Thank You!!

  4. Shahar Y

    Said on April 28, 2008 :

    Hi Fernando,

    the default is english, did you set the Language property to “es”, like that:
    SpellCheck.IsEnabled=“True” Language=“es”

  5. Fernando

    Said on April 29, 2008 :

    Hi Shahar Y
    Yes I did… but doesn’t work :-(
    The only language code (specified in ISO 639) that seems to work is “en” (and “en-us”). Neither Spanish nor French works for me.

  6. Shahar Y

    Said on April 29, 2008 :

    Hi Fernando,

    For some reason it doesn’t work for TextBox. Use RichTextBox instead and set Language=”es-ES”.

    Let me know if it solved your problem.

  7. Fernando

    Said on April 29, 2008 :

    Hi Shahar Y

    It doesn’t work… Not only for spanish, RichTextBox doesn’t work for English!!!

  8. Shahar Y

    Said on April 29, 2008 :


    Not working for English?? This is weird (it works on my machine :-))
    I am sorry, but i am out of ideas.

  9. Fernando

    Said on May 2, 2008 :

    Thank you very much. Now I’m reading that I need resource file of .Net Framework for the language I want to activate…

    I’ll let you know….

    Thanks again…

  10. answer

    Said on May 15, 2008 :

    For RicHTextBox spelling, the language is determined by the Input Language of your operating system.

    You set this with the ‘language bar’ in Windows. Or you can do it programatically using a CultureInfo object like this…

    InputLanguageManager.Current.CurrentInputLanguage = new CultureInfo(“es”);

    You can also iterate through the insalled input languages on your system by doing these ones…

    foreach (CultureInfo curCulture in InputLanguageManager.Current.AvailableInputLanguages)
    …. your code here…

    So in this way, you don’t need to use resource files. But if you’re inputing your text in Spanish, it should have been spell correcting in spanish to begin with. If you don’t know what your RichTextBox is currently set to then use something like…


  11. Diego

    Said on May 8, 2009 :

    Hi, its not work to me too for Spanish Language.
    I need it!! how can I do?? Only works on English!


  12. David

    Said on September 26, 2013 :

    Hi, I’m running an app with .Net Framework 4.0 and have downloaded and installed the Polish language pack (Microsoft .NET Framework 4 Client Profile PLK Language Pack) as suggested in various posts. I have then set the SpellCheck.IsEnabled=”True” for multi-line edit box and spell checking no longer works at all, even with the keyboard input language set to Polish. (although the context menu interestingly is displaying the copy and paste options in the Polish language).

    It was working fine for English prior to this. So what is the current position with .NET 4 … does it support spell checking in different languages other than the default 4 with the relevant language pack installed or not?

2 Trackback(s)

  1. Apr 2, 2008: Dew Drop - April 2, 2008 | Alvin Ashcraft's Morning Dew
  2. Sep 5, 2009: Dawid Po?li?ski » WPF: 90+ Miejsc które warto zna?

Post a Comment