2008-10-14 12 views

Répondre

0

Essayez le code ci-dessous où RTB est RichTextBox:

TextRange tr = new TextRange(rtb.Selection.Start, rtb.Selection.End); 
object oFont = tr.GetPropertyValue(Run.FontFamilyProperty); 
+1

le plus difficile n'a pas été répondu voir ma réponse! – msfanboy

3

j'utiliser le CaretPosition au lieu du début de la sélection et à la fin, comme si le RichTextBox a fait une sélection qui couvre plusieurs domaines de vous formatage obtiendrait DependencyProperty.UnsetValue.

 
TextRange tr = new TextRange(rtb.CaretPosition, rtb.CaretPosition); 
object oFont = tr.GetPropertyValue(Run.FontFamilyProperty); 
3

L'auteur de ce fil a également posé des questions sur TextDecorations où vous n'avez pas fourni des exemples de code et son différent à utiliser. Je posterai cela comme une autre solution :

var obj = _myText.GetPropertyValue(Inline.TextDecorationsProperty); 

        if (obj == DependencyProperty.UnsetValue)     
         IsTextUnderline = false;// mixed formatting 

        if (obj is TextDecorationCollection) 
        { 
         var objProper = obj as TextDecorationCollection; 

         if (objProper.Count > 0)       
          IsTextUnderline = true; // all underlined      
         else       
          IsTextUnderline = false; // nothing underlined     
        } 
1

Voici une solution qui détermine FontWeight, FontStyle, TextDecorations (biffés, souligné) et Super- et Subscripts.

 TextRange textRange = new TextRange(rtb.Selection.Start, rtb.Selection.End); 

     bool IsTextUnderline = false; 
     bool IsTextStrikethrough = false; 
     bool IsTextBold = false; 
     bool IsTextItalic = false; 
     bool IsSuperscript = false; 
     bool IsSubscript = false; 

     // determine underline property 
     if (textRange.GetPropertyValue(Inline.TextDecorationsProperty).Equals(TextDecorations.Strikethrough)) 
      IsTextStrikethrough = true; // all underlined 
     else if (textRange.GetPropertyValue(Inline.TextDecorationsProperty).Equals(TextDecorations.Underline)) 
      IsTextUnderline = true; // all strikethrough 

     // determine bold property 
     if (textRange.GetPropertyValue(Inline.FontWeightProperty).Equals(FontWeights.Bold)) 
      IsTextBold = true; // all bold 

     // determine if superscript or subscript 
     if (textRange.GetPropertyValue(Inline.BaselineAlignmentProperty).Equals(BaselineAlignment.Subscript)) 
      IsSubscript = true; // all subscript 
     else if (textRange.GetPropertyValue(Inline.BaselineAlignmentProperty).Equals(BaselineAlignment.Superscript)) 
      IsSuperscript = true; // all superscript 
Questions connexes