Welcome to MSDN Blogs Sign in | Join | Help

Lester's WPF blog


Simple, easy & beautiful

News

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

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

Posted: Thursday, June 22, 2006 3:35 PM by llester
Attachment(s): Class2.cs

Comments

Ramanujam said:

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

# November 21, 2006 5:41 PM

llester said:

currently we do not support it in V1

# November 21, 2006 11:43 PM
New Comments to this post are disabled
Page view tracker