FlowDocumentReader在其ContextMenu,Copy和Select All中有两个菜单项。我想为它添加一个额外的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.
想法?
答案 0 :(得分:6)
您也可以在xaml中为 FlowDocument 执行此操作:
<FlowDocument.ContextMenu>
<ContextMenu>
<MenuItem Header="{Resx Copy}" Command="Copy"/>
<MenuItem Header="{Resx SelectAll}" Command="SelectAll"/>
<MenuItem Header="{Resx CustomCommand}" Command="{Binding CustomCommand}"/>
</ContextMenu>
</FlowDocument.ContextMenu>
(由于格兰特弗里斯肯的Resx Extension而方便地定位了标题):)
如果需要将其应用于许多FlowDocuments,您还可以在某处以默认样式定义它:
<Style TargetType="FlowDocument">
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu>
<MenuItem Header="{Resx Copy}" Command="Copy"/>
<MenuItem Header="{Resx SelectAll}" Command="SelectAll"/>
<MenuItem Header="{Resx CustomCommand}" Command="{Binding CustomCommand}"/>
</ContextMenu>
</Setter.Value>
</Setter>
</Style>
答案 1 :(得分:0)
我得到的解决方案是简单地在新菜单上重新创建那些MenuItem并取消通常显示的内置ContextMenu的显示。有许多内置的ApplicationCommands可以合并到您自己的自定义ContextMenu中,并且实现起来非常简单。
假设我有一个从某个方法GetContextMenu()创建的ContextMenu,以下事件处理程序拒绝打开内置ContextMenu并将从调用返回的一个替换为GetContextMenu()并添加Copy命令(选择全部类似)。
private void flowDocumentReader_ContextMenuOpening(object sender, ContextMenuEventArgs e)
{
e.Handled = true; // keeps the built-in one from opening
ContextMenu myMenu = GetContextMenu();
MenuItem copyMenuItem = new MenuItem();
copyMenuItem.Command = ApplicationCommand.Copy;
copyMenuItem.CommandTarget = myFlowDocument;
myMenu.Items.Add(copyMenuItem);
ShowMenu(myMenu);
}
private void ShowMenu(ContextMenu menu)
{
menu.Placement = PlacementMode.MousePoint;
menu.PlacementRectangle = new Rect(0.0, 0.0, 0.0, 0.0);
menu.IsOpen = true;
}