看一看MSDN的例子是最好的:
给例子做一下注释:
public MainWindow()
{
InitializeComponent();
myTextBox.ContextMenu = GetContextMenu();//初始化右键菜单(上下文菜单)
}
void tb_ContextMenuOpening(object sender, RoutedEventArgs e)
{
int caretIndex, cmdIndex;
SpellingError spellingError;
myTextBox.ContextMenu = GetContextMenu();//获取默认菜单
caretIndex = myTextBox.CaretIndex;//光标位置
cmdIndex = 0;//菜单项序号
spellingError = myTextBox.GetSpellingError(caretIndex);//获取当前光标处错误,如果没有拼写错误