Lester's WPF\SL Blog

Simple, Easy & Beautiful

WPF: Adding speller to custom context menu

WPF: Adding speller to custom context menu

  • Comments 2

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);

      cmdIndex++;

   }

   Separator separatorMenuItem1 = new Separator();

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

   cmdIndex++;

   MenuItem ignoreAllMI = new MenuItem();

   ignoreAllMI.Header = "Ignore All";

   ignoreAllMI.Command = EditingCommands.IgnoreSpellingError;

   ignoreAllMI.CommandTarget = textBox;

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

   cmdIndex++;

   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 :)..

Attachment: Class2.cs
  • Hi...

    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

  • currently we do not support it in V1

Page 1 of 1 (2 items)