将菜单项添加到 FlowDocumentReader ContextMenu
FlowDocumentReader 的 ContextMenu 中有两个菜单项:复制和全选。我想向其中添加一个额外的 MenuItem 并尝试过此操作:
private void FlowDocumentReader_ContextMenuOpening(object sender, ContextMenuEventArgs e)
{
flowDocumentReader.ContextMenu.Items.Clear();
MenuItem menuItem = new MenuItem();
menuItem.Header = "Test";
flowDocumentReader.ContextMenu.Items.Add(menuItem);
}
另外我也尝试过此操作:
private void FlowDocumentReader_ContextMenuOpening(object sender, ContextMenuEventArgs e)
{
MenuItem menuItem = new MenuItem();
menuItem.Header = "Test";
flowDocumentReader.ContextMenu.Items.Add(menuItem);
}
我没有清除上下文菜单中的项目并尝试附加它。这些都不起作用。
我可以像这样创建自己的菜单:
private void FlowDocumentReader_ContextMenuOpening(object sender, ContextMenuEventArgs e)
{
MenuItem menuItem = new MenuItem();
menuItem.Header = "Test";
flowDocumentReader.ContextMenu.Items.Add(menuItem);
e.Handled = true;
ContextMenu menu = new ContextMenu();
MenuItem a = new MenuItem();
a.Header = "A";
menu.Items.Add(a);
MenuItem b = new MenuItem();
b.Header = "B";
menu.Items.Add(b);
flowDocumentReader.ContextMenu.Items.Clear();
flowDocumentReader.ContextMenu = menu;
menu.IsOpen = true;
}
然后就会显示出来,但我想要的是复制并选择所有菜单项以及 A 和 B。
有想法吗?
The FlowDocumentReader has two menu items in its ContextMenu, Copy and Select All. I'd like to add an additional MenuItem to it and have tried this:
private void FlowDocumentReader_ContextMenuOpening(object sender, ContextMenuEventArgs e)
{
flowDocumentReader.ContextMenu.Items.Clear();
MenuItem menuItem = new MenuItem();
menuItem.Header = "Test";
flowDocumentReader.ContextMenu.Items.Add(menuItem);
}
additionally I've tried this:
private void FlowDocumentReader_ContextMenuOpening(object sender, ContextMenuEventArgs e)
{
MenuItem menuItem = new MenuItem();
menuItem.Header = "Test";
flowDocumentReader.ContextMenu.Items.Add(menuItem);
}
where I don't clear the items in the context menu and attempt to append it. Neither of these work.
I can create my own menu like so:
private void FlowDocumentReader_ContextMenuOpening(object sender, ContextMenuEventArgs e)
{
MenuItem menuItem = new MenuItem();
menuItem.Header = "Test";
flowDocumentReader.ContextMenu.Items.Add(menuItem);
e.Handled = true;
ContextMenu menu = new ContextMenu();
MenuItem a = new MenuItem();
a.Header = "A";
menu.Items.Add(a);
MenuItem b = new MenuItem();
b.Header = "B";
menu.Items.Add(b);
flowDocumentReader.ContextMenu.Items.Clear();
flowDocumentReader.ContextMenu = menu;
menu.IsOpen = true;
}
And that'll show up, but what I'd like to have is the Copy and Select All menu items as well as A and B.
Ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您还可以在 FlowDocument 的 xaml 中执行此操作:(
由于 Grant Frisken 的 Resx Extension) :)
如果需要将其应用于许多 FlowDocuments,您还可以在某处以默认样式定义它:
You could also do this in the xaml for the FlowDocument:
(Headers conveniently localized thanks to Grant Frisken's Resx Extension) :)
If this needs to be applied to many FlowDocuments, you could also define it in a default style somewhere:
我找到的解决方案是简单地在新菜单上重新创建这些 MenuItem,并取消通常显示的内置 ContextMenu 的显示。有许多内置的 ApplicationCommands 可以合并到您的自己的自定义 ContextMenu 及其实现非常简单。
假设我已经通过某种方法 GetContextMenu() 创建了一个 ContextMenu,以下事件处理程序拒绝打开内置 ContextMenu,并替换从调用 GetContextMenu() 返回的菜单,并添加到复制命令中(选择一切都是相似的)。
The solution I arrived at was to simply recreate those MenuItems on the new Menu and cancel the display of the built-in ContextMenu that is normally displayed. There are a number of built-in ApplicationCommands which can be incorporated into your own custom ContextMenu and the implementation of this is quite straightforward.
Assume that I've got a ContextMenu created from some method, GetContextMenu(), the following event handler rejects the opening of the built-in ContextMenu and substitutes the one returned from the call to GetContextMenu() and adds in the Copy command (Select All is similar).