




Попробуйте код ниже, где rtb - это RichTextBox:
TextRange tr = new TextRange(rtb.Selection.Start, rtb.Selection.End);
object oFont = tr.GetPropertyValue(Run.FontFamilyProperty);
Я бы использовал CaretPosition вместо начала и конца выделения, как если бы RichTextBox действительно имел выделение, охватывающее несколько областей форматирования, вы бы получили DependencyProperty.UnsetValue.
TextRange tr = new TextRange(rtb.CaretPosition, rtb.CaretPosition); object oFont = tr.GetPropertyValue(Run.FontFamilyProperty);
Автор этой ветки также спросил о 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
}
Вот решение, которое определяет FontWeight, FontStyle, TextDecorations (зачеркивание, подчеркивание), а также надстрочные и подстрочные индексы.
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
на самое сложное не ответили см. мой ответ!