我有一个RichTextBox,我想添加一个功能 . 基本上我已经启用了 SpellCheck.isEnabled = True
所以每当我拼错一些东西时,红色的波浪线就会显示出来 .
我要添加的功能是"Add Word"功能 . 基本上我跟着这个Tutorial,它向你展示了如何做到这一点 . 本教程使用 WPF TextBox
而不是 WPF RichTextBox
所以我所做的就是尽力让它适用于 WPF RichTextBox
. 我的代码是:
private void ctrl_RtfText_ContextMenuOpening(object sender, ContextMenuEventArgs e)
{
int index = 0;
this.ctrl_RtfText.ContextMenu.Items.Clear(); //Clearing the existing items
//Getting the spellcheck suggestions.
SpellingError spellingError = this.ctrl_RtfText.GetSpellingError(this.ctrl_RtfText.CaretPosition);
if (spellingError != null && spellingError.Suggestions.Count() >= 1)
{
//Creating the suggestions menu items.
foreach (string suggestion in spellingError.Suggestions)
{
MenuItem menuItem = new MenuItem();
menuItem.Header = suggestion;
menuItem.FontWeight = FontWeights.Bold;
menuItem.Command = EditingCommands.CorrectSpellingError;
menuItem.CommandParameter = suggestion;
menuItem.CommandTarget = this.ctrl_RtfText;
this.ctrl_RtfText.ContextMenu.Items.Insert(index, menuItem);
index++;
}
Separator seperator = new Separator();
this.ctrl_RtfText.ContextMenu.Items.Insert(index, seperator);
index++;
//Adding the IgnoreAll menu item
MenuItem IgnoreAllMenuItem = new MenuItem();
IgnoreAllMenuItem.Header = "Ignore All";
IgnoreAllMenuItem.Command = EditingCommands.IgnoreSpellingError;
IgnoreAllMenuItem.CommandTarget = this.ctrl_RtfText;
this.ctrl_RtfText.ContextMenu.Items.Insert(index, IgnoreAllMenuItem);
index++;
}
else
{
//No Suggestions found, add a disabled NoSuggestions menuitem.
MenuItem menuItem = new MenuItem();
menuItem.Header = "No Suggestions";
menuItem.IsEnabled = false;
this.ctrl_RtfText.ContextMenu.Items.Insert(index, menuItem);
index++;
}
//.Net 4.0 Supports CustomDictionaries, Option for Adding to dictionary.
TextPointer selectionStart = ctrl_RtfText.Document.ContentStart;
if (!(this.ctrl_RtfText.Selection.IsEmpty))
{
Separator seperator1 = new Separator();
this.ctrl_RtfText.ContextMenu.Items.Insert(index, seperator1);
index++;
MenuItem AddToDictionary = new MenuItem();
AddToDictionary.Header = "Add to Dictionary";
//Getting the word to add
this.ctrl_RtfText.GetSpellingErrorRange(selectionStart);
//Ignoring the added word.
AddToDictionary.Command = EditingCommands.IgnoreSpellingError;
AddToDictionary.CommandTarget = this.ctrl_RtfText;
AddToDictionary.Click += (object o, RoutedEventArgs rea) =>
{
this.AddToDictionary(this.ctrl_RtfText.Selection.Text);
};
this.ctrl_RtfText.ContextMenu.Items.Insert(index, AddToDictionary);
index++;
}
//Common Edit MenuItems.
Separator seperator2 = new Separator();
this.ctrl_RtfText.ContextMenu.Items.Insert(index, seperator2);
index++;
//Cut
MenuItem cutMenuItem = new MenuItem();
cutMenuItem.Command = ApplicationCommands.Cut;
this.ctrl_RtfText.ContextMenu.Items.Insert(index, cutMenuItem);
index++;
//Copy
MenuItem copyMenuItem = new MenuItem();
copyMenuItem.Command = ApplicationCommands.Copy;
this.ctrl_RtfText.ContextMenu.Items.Insert(index, copyMenuItem);
index++;
//Paste
MenuItem pasteMenuItem = new MenuItem();
pasteMenuItem.Command = ApplicationCommands.Paste;
this.ctrl_RtfText.ContextMenu.Items.Insert(index, pasteMenuItem);
index++;
Separator seperator3 = new Separator();
this.ctrl_RtfText.ContextMenu.Items.Insert(index, seperator3);
index++;
//Delete
MenuItem deleteMenuItem = new MenuItem();
deleteMenuItem.Command = ApplicationCommands.Delete;
this.ctrl_RtfText.ContextMenu.Items.Insert(index, deleteMenuItem);
index++;
Separator seperator4 = new Separator();
this.ctrl_RtfText.ContextMenu.Items.Insert(index, seperator4);
index++;
//Select All
MenuItem selectAllMenuItem = new MenuItem();
selectAllMenuItem.Command = ApplicationCommands.SelectAll;
this.ctrl_RtfText.ContextMenu.Items.Insert(index, selectAllMenuItem);
index++;
}
//Method to Add text to Dictionary
private void AddToDictionary(string entry)
{
using (StreamWriter streamWriter = new StreamWriter(@"C:\Users\xxx\Desktop\New folder (2)\MyCustomDictionary.lex", true))
{
streamWriter.WriteLine(entry);
}
}
Now the Problem:
基本上是拼写检查器,当拼写检查器拼写错误时,右键单击该单词时,会显示上下文菜单并显示建议 . 用户可以选择其中一个建议和单词修复 . 这一切都发生在用户不需要突出显示该单词然后选择建议的情况下 . 让我说清楚:
用户点击首选建议并进行更正 . 现在,我的代码基本上只在选择文本时显示“添加单词”功能:
我想要它,就像上一张图片一样,当出现错误时,“添加单词”功能应该与其他建议等一起出现 .
2 回答
下载一个EN_US.lang,然后让它读取文件,添加单词然后注释,如果你想让我告诉你如何做到这一点!
要修复此错误,请更换
同
这是一个快速编辑 . 不确定它是否运行没有一些小调整 .