WPF: Adding speller to custom context menu

One of the interesting features in Editing is the speller and it works really well. :) One of the ineteresting scenarios when it comes to speller is to add the speller choices to a custom context menu. The speller choices would have to be added in the ContextMenuOpening event handler as below:


spellingError = textBox.GetSpellingError(caretIndex);

if (spellingError != null)


   foreach (string str in spellingError.Suggestions)


      MenuItem mi = new MenuItem();

      mi.Header = str;

      mi.FontWeight = FontWeights.Bold;

      mi.Command = EditingCommands.CorrectSpellingError;

      mi.CommandParameter = str;

      mi.CommandTarget = textBox;

      textBox.ContextMenu.Items.Insert(cmdIndex, mi);



   Separator separatorMenuItem1 = new Separator();

   textBox.ContextMenu.Items.Insert(cmdIndex, separatorMenuItem1);


   MenuItem ignoreAllMI = new MenuItem();

   ignoreAllMI.Header = “Ignore All”;

   ignoreAllMI.Command = EditingCommands.IgnoreSpellingError;

   ignoreAllMI.CommandTarget = textBox;

   textBox.ContextMenu.Items.Insert(cmdIndex, ignoreAllMI);


   Separator separatorMenuItem2 = new Separator();

   textBox.ContextMenu.Items.Insert(cmdIndex, separatorMenuItem2);




Thats all there is to it. Its the same for RichTextBox except for the first line where you get the spellingError – rtb.GetSpellingError(rtb.CaretPosition)

The complete sample code is attached so that you can see it working in all glory :)..


Comments (2)

  1. Ramanujam says:


    What do i ned to do similarly for adding the word to dictionary… like the ‘Add To Dictionary’ option in word.

    please reply back to ramanujamsampath@gmail.com

  2. llester says:

    currently we do not support it in V1