.net - 将菜单项添加到默认上下文菜单

标签 .net wpf contextmenu menuitem

我想在默认 ContextMenu 中添加一个菜单项的 RichTextBox .

我可以创建一个新的上下文菜单,但随后我丢失了默认菜单中显示的拼写检查建议。

有没有办法在不重新实现所有内容的情况下添加项目?

最佳答案

用拼写建议、剪切、粘贴等重新实现 RichTextBox 上下文菜单并不太难。

如下连接上下文菜单打开事件:

AddHandler(RichTextBox.ContextMenuOpeningEvent, new ContextMenuEventHandler(RichTextBox_ContextMenuOpening), true);

在事件处理程序中根据需要构建上下文菜单。您可以使用以下内容重新创建现有的上下文菜单菜单项:

私有(private) IList GetSpellingSuggestions()
{
List 拼写建议 = new List();
SpellingError spellingError = myRichTextBox.GetSpellingError(myRichTextBox.CaretPosition);
如果(拼写错误!= null)
{
foreach(spellingError.Suggestions 中的字符串 str)
{
菜单项 mi = new MenuItem();
mi.Header = str;
mi.FontWeight = FontWeights.Bold;
mi.Command = EditingCommands.CorrectSpellingError;
mi.CommandParameter = str;
mi.CommandTarget = myRichTextBox;
拼写建议。添加(mi);
}
}
返回拼写建议;
}

私有(private) IList GetStandardCommands()
{
List standardCommands = new List();

菜单项项 = 新菜单项();
item.Command = ApplicationCommands.Cut;
标准命令。添加(项目);

项目 = 新菜单项();
item.Command = ApplicationCommands.Copy;
标准命令。添加(项目);

项目 = 新菜单项();
item.Command = ApplicationCommands.Paste;
标准命令。添加(项目);

返回标准命令;
}

如果有拼写错误,您可以使用以下命令创建 Ignore All:

MenuItem ignoreAllMI = new MenuItem();
ignoreAllMI.Header = "全部忽略";
ignoreAllMI.Command = EditingCommands.IgnoreSpellingError;
忽略AllMI.CommandTarget = 文本框;
newContextMenu.Items.Add(ignoreAllMI);

根据需要添加分隔符。将它们添加到新上下文菜单的项目中,然后添加 Shiny 的新 MenuItems。

不过,我将继续寻找一种获取实际上下文菜单的方法,因为这与我将在不久的将来进行的工作有关。

关于.net - 将菜单项添加到默认上下文菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/210634/

相关文章:

android ListView 上下文菜单

c# - 为什么我的 .NET 应用程序在从网络驱动器运行时会崩溃?

wpf - 是否有官方的 Windows Presentation Foundation (WPF) Logo ?

javascript - Chrome Extension 无法让 ContextMenu 仅加载特定页面

c# - 如何在 WPF 中实现这种发光效果?

c# - 判断事件窗口是否为 WPF 窗口

javascript - 在图标上显示上下文菜单?

Linux 上的 .NET 门户网站和 SSRS

c# - .NET:向 WebClient 的 UploadStringCompletedEventHandler 提交用户定义标记的最佳方式是什么

c# - 异步方法 C# WhenAll 问题