2008-10-14 42 views

回答

0

嘗試以下其中RTB是RichTextBox的代碼:

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

最困難的是沒有回答看到我的答案! – msfanboy 2010-08-10 21:22:24

3

我會使用,而不是選擇開始和結束的CaretPosition,彷彿RichTextBox中實際上有一個選擇跨越格式化你的多個區域會得到DependencyProperty.UnsetValue。

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

該主題的作者還詢問了有關TextDecorations的情況,您未提供示例代碼及其使用的不同。我張貼此作爲進一步溶液

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

這裏是確定fontWeight設置,FontStyle,TextDecorations(刪除線和下劃線)和的Super和下標中的溶液。

 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