添加菜单项默认的上下文菜单 [英] add menu item to default context menu
问题描述
我想一个菜单项添加到一个RichTextBox的默认文本菜单。我可以创建一个新的上下文菜单,但后来我失去了显示在默认菜单的拼写检查的建议。有没有一种方法来添加一个项目而无需重新实现一切吗?
I'd like to add a menu item to the default ContextMenu of a RichTextBox. I can create a new context menu but then I loose the spell check suggestions that show up in the default menu. Is there a way to add an item without re-implementing everything?
推荐答案
这不是太靠谱重新实现与拼写建议,剪切,粘贴,RichTextBox的上下文菜单等。
It's not too tricky to reimplement the RichTextBox context menu with spelling suggestions, Cut, Paste, etc.
挂钩的上下文菜单中开幕活动如下:
Hook up the context menu opening event as follows:
AddHandler(RichTextBox.ContextMenuOpeningEvent, new ContextMenuEventHandler(RichTextBox_ContextMenuOpening), true);
在事件处理程序构建上下文菜单中,你所需要的。您可以创建具有以下现有的上下文菜单中的菜单项:
Within the event handler build the context menu as you need. You can recreate the existing context menu menu items with the following:
private IList<MenuItem> GetSpellingSuggestions()
{
List<MenuItem> spellingSuggestions = new List();
SpellingError spellingError = myRichTextBox.GetSpellingError(myRichTextBox.CaretPosition);
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 = myRichTextBox;
spellingSuggestions.Add(mi);
}
}
return spellingSuggestions;
}
private IList<MenuItem> GetStandardCommands()
{
List<MenuItem> standardCommands = new List();
MenuItem item = new MenuItem();
item.Command = ApplicationCommands.Cut;
standardCommands.Add(item);
item = new MenuItem();
item.Command = ApplicationCommands.Copy;
standardCommands.Add(item);
item = new MenuItem();
item.Command = ApplicationCommands.Paste;
standardCommands.Add(item);
return standardCommands;
}
如果有拼写错误,你可以创造全部忽略与:
If there are spelling errors, you can create Ignore All with:
MenuItem ignoreAllMI = new MenuItem();
ignoreAllMI.Header = "Ignore All";
ignoreAllMI.Command = EditingCommands.IgnoreSpellingError;
ignoreAllMI.CommandTarget = textBox;
newContextMenu.Items.Add(ignoreAllMI);
添加分隔符的要求。添加这些新的上下文菜单中的项,然后添加你闪亮的新的MenuItems。
Add separators as required. Add those to the new context menu's items, and then add your shiny new MenuItems.
我要不停地寻找一种方式来获得实际的上下文菜单,虽然,因为这是有关的东西,我会努力在不久的将来。
I'm going to keep looking for a way to obtain the actual context menu though, as this is relevant to something I'll be working on in the near future.
这篇关于添加菜单项默认的上下文菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!