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

Class2.cs