How can I detect the current text formatting at the cursor position in a WPF RichTextBox?
A:
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);
Artur Carvalho
2008-10-14 20:48:04
the most difficult was not answered see my answer!
msfanboy
2010-08-10 21:22:24
+2
A:
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);
Donnelle
2008-10-15 01:45:27
A:
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
}
msfanboy
2010-08-10 21:24:09