如何:使用上下文菜单中的拼写检查功能
更新:2007 年 11 月
默认情况下,当在类似 TextBox 或 RichTextBox 这样的编辑控件中启用拼写检查功能时,可以在上下文菜单中获得拼写检查选项。例如,当用户右击一个拼错的单词时,会得到一组拼写建议或“全部忽略”选项。不过,当您用自己的自定义上下文菜单覆盖默认上下文菜单时,将会丢失此功能,而需要编写代码才能在上下文菜单中重新启用拼写检查功能。下面的示例演示如何在 TextBox 中启用此功能。
示例
下面的示例演示利用用于实现上下文菜单的某些事件创建 TextBox 的可扩展应用程序标记语言 (XAML)。
<Page x:Class="SDKSample.SpellerCustomContextMenu"
xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
Loaded="OnWindowLoaded">
<TextBox
Name="myTextBox"
TextWrapping="Wrap"
SpellCheck.IsEnabled="True"
ContextMenuOpening="tb_ContextMenuOpening">
In a custum menu you need to write code to add speler choices
because everything in a custom context menu has to be added explicitly.
</TextBox>
</Page>
下面的示例演示实现上下文菜单的代码。
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace SDKSample
{
public partial class SpellerCustomContextMenu : Page
{
void OnWindowLoaded(object sender, RoutedEventArgs e)
{
//This is required for the first time ContextMenu invocation so that TextEditor doesnt handle it.
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);
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 = myTextBox;
myTextBox.ContextMenu.Items.Insert(cmdIndex, mi);
cmdIndex++;
}
Separator separatorMenuItem1 = new Separator();
myTextBox.ContextMenu.Items.Insert(cmdIndex, separatorMenuItem1);
cmdIndex++;
MenuItem ignoreAllMI = new MenuItem();
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);
}
}
// Gets a fresh context menu.
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";
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;
}
}
}
用于在 RichTextBox 中执行上述操作的代码是类似的。主要区别在于传递给 GetSpellingError 方法的参数。对于 TextBox,传递的是插入符号位置的整数索引:
spellingError = myTextBox.GetSpellingError(caretIndex);
对于 RichTextBox,传递的是指定插入符号位置的 TextPointer:
spellingError = myRichTextBox.GetSpellingError(myRichTextBox.CaretPosition);