自定义ContextMenu用于RichTextBox中的图像

时间:2016-09-15 13:26:32

标签: wpf image contextmenu richtextbox

用户可以将图片嵌入RichTextBox - 我将其添加到InlineUIContainer 我已向ContextMenu添加了自定义InlineUIContainer,但右键单击时为 出现标准的RTB-contextmenu(剪切,复制,粘贴) - 而不是自定义的 为什么,我该如何解决这个问题?

  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"];

1 个答案:

答案 0 :(得分:0)

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

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

        }