将菜单项添加到 FlowDocumentReader ContextMenu

发布于 2024-10-17 07:11:22 字数 1463 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吃兔兔 2024-10-24 07:11:22

您还可以在 FlowDocument 的 xaml 中执行此操作:(

<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>

由于 Grant Frisken 的 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>

You could also do this in the xaml for the 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>

(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:

<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>
挽清梦 2024-10-24 07:11:22

我找到的解决方案是简单地在新菜单上重新创建这些 MenuItem,并取消通常显示的内置 ContextMenu 的显示。有许多内置的 ApplicationCommands 可以合并到您的自己的自定义 ContextMenu 及其实现非常简单。

假设我已经通过某种方法 GetContextMenu() 创建了一个 ContextMenu,以下事件处理程序拒绝打开内置 ContextMenu,并替换从调用 GetContextMenu() 返回的菜单,并添加到复制命令中(选择一切都是相似的)。

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;
}

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).

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文