Select Range of Text in WPF RichTextBox (FlowDocument) Programmatically
I have this WPF RichTextBox and I want to programmatically select a given range of letters/words and highlight it. I've tried this, but it doesn't work, probably because I'm not taking into account some hidden FlowDocument tags or similar. For example, I want to select letters 3-8 but 2-6 gets selected): var start = MyRichTextBox.Document.ContentStart; var startPos = start.GetPositionAtOffset(3); var endPos = start.GetPositionAtOffset(8); var textRange = new TextRange(startPos,endPos); textRange.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(Colors.Blue)); textRange