2009-08-27 9 views
0

HI, Je développe un éditeur en utilisant RichTextBox dans WPF, je dois implémenter la fonctionnalité que l'utilisateur peut définir la police de Text sélectionné si du texte est sélectionné, si rien n'est sélectionné alors la police doit être définie pour nouveau texte. Si je définis les propriétés de police (comme FontStyle, FontSize) de RTB dans un cas ultérieur, il définira les propriétés pour le texte intégral, comment puis-je définir les propriétés de police pour le nouveau texte (si l'utilisateur entre le texte).Définir les propriétés des polices dans RichTextBox

Répondre

5

J'ai implémenté une barre d'outils qui peut changer la taille de police, la famille, la couleur, etc. Ce que j'ai trouvé est que les détails peuvent être difficiles avec le wpf richtextbox. La définition de la police de sélection a du sens, mais il existe également les propriétés de police par défaut de la zone de texte et les propriétés de caret actuelles à prendre en compte. Voici ce que j'ai écrit pour le faire fonctionner dans la plupart des cas avec la taille de la police. Le processus devrait être le même pour fontfamily et fontcolor. J'espère que cela aide.

public static void SetFontSize(RichTextBox target, double value) 
    { 
     // Make sure we have a richtextbox. 
     if (target == null) 
      return; 

     // Make sure we have a selection. Should have one even if there is no text selected. 
     if (target.Selection != null) 
     { 
      // Check whether there is text selected or just sitting at cursor 
      if (target.Selection.IsEmpty) 
      { 
       // Check to see if we are at the start of the textbox and nothing has been added yet 
       if (target.Selection.Start.Paragraph == null) 
       { 
        // Add a new paragraph object to the richtextbox with the fontsize 
        Paragraph p = new Paragraph(); 
        p.FontSize = value; 
        target.Document.Blocks.Add(p); 
       } 
       else 
       { 
        // Get current position of cursor 
        TextPointer curCaret = target.CaretPosition; 
        // Get the current block object that the cursor is in 
        Block curBlock = target.Document.Blocks.Where 
         (x => x.ContentStart.CompareTo(curCaret) == -1 && x.ContentEnd.CompareTo(curCaret) == 1).FirstOrDefault(); 
        if (curBlock != null) 
        { 
         Paragraph curParagraph = curBlock as Paragraph; 
         // Create a new run object with the fontsize, and add it to the current block 
         Run newRun = new Run(); 
         newRun.FontSize = value; 
         curParagraph.Inlines.Add(newRun); 
         // Reset the cursor into the new block. 
         // If we don't do this, the font size will default again when you start typing. 
         target.CaretPosition = newRun.ElementStart; 
        } 
       } 
      } 
      else // There is selected text, so change the fontsize of the selection 
      { 
       TextRange selectionTextRange = new TextRange(target.Selection.Start, target.Selection.End); 
       selectionTextRange.ApplyPropertyValue(TextElement.FontSizeProperty, value); 
      } 
     } 
     // Reset the focus onto the richtextbox after selecting the font in a toolbar etc 
     target.Focus(); 
    } 
+0

belle réponse –

Questions connexes