I am applying formatting to selected tokens in a WPF RichTextBox. To do this I get a TextRange that encompasses the token that I would like to highlight. I will then change the color of the text like this:
// Get start and end pointer for token
TextPointer startPointer = run.ContentStart.GetPositionAtOffset(startOffset);
TextPointer endPointer = run.ContentStart.GetPositionAtOffset(endOffset);
// Get text range for token
TextRange textRange = new TextRange(startPointer, endPointer);
// Highlight token
textRange.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Blue);
This is happening on the TextChanged event of my RichTextBox.
The formatting is applied as expected, but continuing to type text will result in the new text inheriting the formatting that has already been applied to the adjacent word. I would like the formatting of any new text to use the default formatting options defined in the RichTextBox properties. Is this possible?
Alternatively I could highlight all tokens that I don't want be blue with the default formatting options but this feels awkward to me.