2010-08-16 7 views

Répondre

4

... Résolu

if (this.TextEditor.Selection.IsEmpty) 
    this.TextEditor.CurrentFontFamily = SelectedFont; 
else 
    this.TextEditor.Selection.ApplyPropertyValue(TextElement.FontFamilyProperty, SelectedFont); 
+0

Cette solution est basée sur: http://stackoverflow.com/questions/1854703/how-to-set-richtextbox-font-for-the-next-text-to-be-written – tgr42

6

Que diriez-vous quelque chose comme:

TextSelection text = richTextBox.Selection; 
if (!text.IsEmpty) 
{ 
    text.ApplyPropertyValue(RichTextBox.FontSizeProperty, value); 
} 
+0

C'est pour la taille de police au moins. Theres probablement une propriété pour la famille de police si c'est ce que vous vouliez changer. – KrisTrip

+0

Presque là. Cela fonctionne pour la taille de la police. Mais pour la famille de polices, cela change tout le paragraphe et pas seulement la sélection. –

9

J'ai mis en place une barre d'outils qui peut changer la taille de la police, la famille, la couleur, etc. Ce que je trouve est les détails peuvent être délicat avec le richtextbox WPF. La définition de la police de sélection est logique, 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(); 
    } 
2

Pour changer la famille de polices pour une sélection dans la RichTextBox vous devez utiliser ceci:

text.ApplyPropertyValue(Run.FontFamilyProperty, value); 

Le texte sélectionné dans un RichTextBox est un objet Run, donc il faut utiliser les propriétés de dépendance Run. Cela semble fonctionner dans Silverlight au moins, ce devrait donc être la même chose dans WPF.

3

Pour obtenir l'utilisation de la sélection actuelle:

Dim RNG Comme TextRange = New TextRange (YourRtfBox.Selection.Start, YourRtfBox.Selection.End)

puis définissez les fontstyle:

RNG .ApplyPropertyValue (Inline.FontSizeProperty, YourFontSizeValue) rng.ApplyPropertyValue (Inline.FontFamilyProperty, YourFontFamilyValue)

Questions connexes