RichTextBox 中图像的自定义上下文菜单

Custom ContextMenu for images in a RichTextBox

用户可以将图像嵌入到 RichTextBox - 我将它们添加到 InlineUIContainer
我已将自定义 ContextMenu 添加到 InlineUIContainer,但当右键单击时
出现标准 RTB 上下文菜单(剪切、复制、粘贴)- 而不是自定义菜单。
为什么,我该如何解决这个问题?

  string fileName = openFileDialog.FileName;

  BitmapImage bitmap = new BitmapImage(new Uri(fileName, UriKind.Absolute));

  Image image = new Image();
  image.Source = bitmap;
  image.Width = bitmap.Width;
  image.Height = bitmap.Height;

  InlineUIContainer pix = new InlineUIContainer(image, rt.CaretPosition);
  pix.BaselineAlignment = BaselineAlignment.Center;
  pix.ContextMenu = (ContextMenu)this.Resources["imageContext"];

好的,这行得通 - 我必须重新创建所有 "standard" 上下文菜单项,包括拼写检查项(幸运的是我找到了一个例子 :)

    private void rt_ContextMenuOpening(object sender, ContextMenuEventArgs e)
        {
        int index = 0;
        MenuItem menuItem;
        this.rt.ContextMenu.Items.Clear(); //Clearing the existing items

// right clicked on an image ?
        int offZstart, offZend;
        activeImg = null;
        foreach (Block block in rt.Document.Blocks)
            {
            Paragraph p = block as Paragraph;
            if (p != null)
                {
                foreach (Inline inline in p.Inlines)
                    {
                    InlineUIContainer iuic = inline as InlineUIContainer;
                    if (iuic != null)
                        {
                        offZstart   = rt.Selection.Start.GetOffsetToPosition(iuic.ContentStart);
                        offZend     = rt.Selection.End.GetOffsetToPosition(iuic.ContentEnd);
                        //  if (rt.Selection.Contains(iuic.ContentStart))
                        if ((offZstart == 2  || offZstart ==  1) && (offZend   == -2 || offZend   == -1))
                            {
                            if (iuic.Child is Border) // I wrap my images in borders
                                {
                                Border border = (Border)iuic.Child;

                                activeImg = (Image)border.Child;

                                MenuItem imageMenu = new MenuItem();
                                imageMenu.Header = "Image..";
                                this.rt.ContextMenu.Items.Insert(index++, imageMenu);

                                MenuItem borderItem = new MenuItem();
                                borderItem.Header = "Border";
                                borderItem.Click += imgBorderWidth;
                                imageMenu.Items.Add(borderItem);

                                MenuItem marginItem = new MenuItem();
                                marginItem.Header = "Margin";
                                marginItem.Click += imgMarginWidth;
                                imageMenu.Items.Add(marginItem);

                                MenuItem paddingItem = new MenuItem();
                                paddingItem.Header = "Padding";
                                paddingItem.Click += imgPaddingWidth;
                                imageMenu.Items.Add(paddingItem);
                                }
                            }
                        }
                    }
                }
            }

// spellchecking
        SpellingError spellingError = this.rt.GetSpellingError(this.rt.CaretPosition);
        if (spellingError != null && spellingError.Suggestions.Count() >= 1)
            {
            //Creating the suggestions menu items.
            foreach (string suggestion in spellingError.Suggestions)
                {
                menuItem = new MenuItem();
                menuItem.Header = suggestion;
                menuItem.FontWeight = FontWeights.Bold;
                menuItem.Command = EditingCommands.CorrectSpellingError;
                menuItem.CommandParameter = suggestion;
                menuItem.CommandTarget = this.rt;
                this.rt.ContextMenu.Items.Insert(index++, menuItem);
                }

            //Getting the word to add/ignore
            var word = this.rt.GetSpellingErrorRange(this.rt.CaretPosition);

            this.rt.ContextMenu.Items.Insert(index++, new Separator());
            //Adding the IgnoreAll menu item
            MenuItem IgnoreAllMenuItem = new MenuItem();
            IgnoreAllMenuItem.Header = "Ignore All ''"+word.Text+"''";
            IgnoreAllMenuItem.Command = EditingCommands.IgnoreSpellingError;
            IgnoreAllMenuItem.CommandTarget = this.rt;
            this.rt.ContextMenu.Items.Insert(index++, IgnoreAllMenuItem);

            this.rt.ContextMenu.Items.Insert(index++, new Separator());
            //Add as new word in dictionary
            MenuItem AddToDictionary = new MenuItem();
            AddToDictionary.Header = "Add ''" + word.Text + "'' to dictionary";
            AddToDictionary.Command = EditingCommands.IgnoreSpellingError;
            AddToDictionary.CommandTarget = this.rt;
            AddToDictionary.Click += (object o, RoutedEventArgs rea) =>
            {
                //this.AddToDictionary(word.Text);
                MessageBox.Show("Want to add ''" + word.Text + "'' to dictionary\n- but don't know how..");
            };
            this.rt.ContextMenu.Items.Insert(index++, AddToDictionary);

            this.rt.ContextMenu.Items.Insert(index++, new Separator());

            }


        //Cut
        MenuItem cutMenuItem = new MenuItem();
        cutMenuItem.Command = ApplicationCommands.Cut;
        this.rt.ContextMenu.Items.Insert(index++, cutMenuItem);

        //Copy
        MenuItem copyMenuItem = new MenuItem();
        copyMenuItem.Command = ApplicationCommands.Copy;
        this.rt.ContextMenu.Items.Insert(index++, copyMenuItem);

        //Paste
        MenuItem pasteMenuItem = new MenuItem();
        pasteMenuItem.Command = ApplicationCommands.Paste;
        this.rt.ContextMenu.Items.Insert(index++, pasteMenuItem);

        this.rt.ContextMenu.Items.Insert(index++, new Separator());

        //Delete
        MenuItem deleteMenuItem = new MenuItem();
        deleteMenuItem.Command = EditingCommands.Delete;
        this.rt.ContextMenu.Items.Insert(index++, deleteMenuItem);

        this.rt.ContextMenu.Items.Insert(index++, new Separator());

        //Select All
        MenuItem selectAllMenuItem = new MenuItem();
        selectAllMenuItem.Command = ApplicationCommands.SelectAll;
        this.rt.ContextMenu.Items.Insert(index++, selectAllMenuItem);

        }