Get FontWeight/FontStyle/TextDecorations from WPF RichTextBox

前端 未结 4 417
渐次进展
渐次进展 2021-01-14 13:02

How can I detect the current text formatting at the cursor position in a WPF RichTextBox?

相关标签:
4条回答
  • 2021-01-14 13:25

    The author of this thread also asked about TextDecorations where you did not provide sample code and its different to use. I post this as a further 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                   
                        } 
    
    0 讨论(0)
  • 2021-01-14 13:30

    Try the code below where rtb is the RichTextBox:

    TextRange tr = new TextRange(rtb.Selection.Start, rtb.Selection.End);
    object oFont = tr.GetPropertyValue(Run.FontFamilyProperty);
    
    0 讨论(0)
  • 2021-01-14 13:31

    Here is a solution that determines FontWeight, FontStyle, TextDecorations (strikethrough, underline) and Super- and 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
    
    0 讨论(0)
  • 2021-01-14 13:37

    I'd use the CaretPosition instead of the selection start and end, as if the RichTextBox actually has a selection that spans multiple areas of formatting you would get DependencyProperty.UnsetValue.

    TextRange tr = new TextRange(rtb.CaretPosition, rtb.CaretPosition);
    object oFont = tr.GetPropertyValue(Run.FontFamilyProperty);
    
    0 讨论(0)
提交回复
热议问题