Change Style of TextSelection on WPF RichTextBox

Viewed 19

I have a WPF RichTextBox and i'm trying to do is change the style: FontFamily, FontSize, FontWeight, Color and some more.

I can change some of this values on the selected Text, but i can't find a TextDecorationProperty

CodeExample:

TextSelection textRange = richTextBoxWpf.Selection;                
if (btBold.IsChecked ?? false)
{
textRange.ApplyPropertyValue(FontWeightProperty, FontWeights.Bold);
btBold.IsChecked = true;
}
else
{
btBold.IsChecked = false;
textRange.ApplyPropertyValue(FontWeightProperty, FontWeights.Normal);
}

My code fails on set the Property:

var pens = new System.Windows.Media.Pen(System.Windows.Media.Brushes.Black, 1);
textRange.ApplyPropertyValue(TextDecoration.PenProperty, pens);

Fail Error-Message: "Die Pen-Eigenschaft ist für die Textformatierung nicht gültig."

1 Answers

Sorry, after a long search I found the Solution myself:

It in the Documents.Inline class:

TextSelection textRange = richTextBoxWpf.Selection;

textRange.ApplyPropertyValue(Inline.TextDecorationsProperty, TextDecorations.Underline);
Related