2016-09-15 2 views
0

Les utilisateurs peuvent incorporer des images à un RichTextBox - Je les ajoute dans un InlineUIContainer.
J'ai ajouté un ContextMenu personnalisé au InlineUIContainer, mais lorsque vous cliquez avec le bouton droit sur
, le menu contextuel RTB standard (couper, copier, coller) apparaît - pas celui personnalisé.
Pourquoi, et comment puis-je résoudre ce problème?Menu contextuel personnalisé pour les images dans un RichTextBox

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

Vous devriez probablement abonner à l'événement 'ContextMenuOpening' de' RichTextBox' et utilisez votre menu contextuel personnalisé si vous avez déterminé que le clic droit s'est produit sur l'image. – Sam

+0

Merci Sam, je vais regarder ça:) – T4NK3R

Répondre

0

droit, cela fonctionne - je devais recréer les tous les éléments du menu contextuel « standard », y compris ceux de vérification orthographique (heureusement que je trouve un exemple de ce qui suit:)

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

     }