Take a look at the MSDN example for the best:
Http://msdn.microsoft.com/zh-cn/library/vstudio/system.windows.controls.primitives.textboxbase.spellcheck (v= vs.100). aspx
Give an example a comment:
Public MainWindow () {InitializeComponent (); Mytextbox.contextmenu = Getcontextmenu ();//Initialize Right-click menu (Context menu)} void Tb_contextmenuopening (object
sender, RoutedEventArgs e) {int caretindex, cmdindex;
Spellingerror Spellingerror; Mytextbox.contextmenu = Getcontextmenu ();//get default Menu Caretindex = mytextbox.caretindex;//cursor position Cmdind ex = 0;//menu item Ordinal spellingerror = Mytextbox.getspellingerror (Caretindex);//Gets the current cursor error, or null if there is no spelling error F (spellingerror! = null) {//correct spelling of each recommended spelling error, create a menu item foreach (String str in
spellingerror.suggestions) {MenuItem mi = new MenuItem (); Mi. Header = The text content of the str;//menu item mi.
FontWeight = Fontweights.bold;
Mi.command = editingcommands.correctspellingerror;//menu item Clicking on the executed command is a command that changes the spelling error at the current cursor. Mi. CommandParameter = str;//The parameter of this command mi. Commandtarget = mytextbox;//This command executes the target text box MyTextBox.ContextMenu.Items.Insert (Cmdindex, MI);//Add the item to the right-click menu Ordinal position cmdindex++;//sequence number increment} Separator separatorMenuItem1 = new Separator (
);
MyTextBox.ContextMenu.Items.Insert (Cmdindex, separatorMenuItem1);//Insert a split horizontal line cmdindex++;
MenuItem Ignoreallmi = new MenuItem ();//Create a new menu item that ignores all spelling errors Ignoreallmi.header = "Ignore all";
Ignoreallmi.command = Editingcommands.ignorespellingerror;
Ignoreallmi.commandtarget = MyTextBox;
MyTextBox.ContextMenu.Items.Insert (Cmdindex, Ignoreallmi);
cmdindex++;
Separator separatorMenuItem2 = new Separator ();
MyTextBox.ContextMenu.Items.Insert (Cmdindex, separatorMenuItem2);//Insert a split line, followed by the default menu item.
}
} Private ContextMenu Getcontextmenu () {ContextMenu cm = new ContextMenu ();
Can Create STATIC custom menu items if exists here ...
MenuItem M1, M2, M3, M4;
M1 = new MenuItem (); M1.
Header = "File";//m1.command = Applicationcommands.save;
m2 = new MenuItem (); M2.
Header = "Save";
M3 = new MenuItem (); M3.
Header = "SaveAs";
M4 = new MenuItem (); M4.
Header = "recent Files"; Can add functionality for the custom menu items here ... cm.
Items.Add (M1); Cm.
Items.Add (m2); Cm.
Items.Add (m3); Cm.
Items.Add (M4);
return cm; }
The user dictionary can be defined in the SpellCheck customdictionaries, and the spelling checker ignores these words.
Http://www.cnblogs.com/gnielee/archive/2010/05/04/wpf4-spellcheck.html This article gives a detailed example.
Then we study the Spellingerror class to see if we can play any tricks.