private static TextPointer Highlight(FlowDocument document, Interval interval) { if (document == null) throw new ArgumentNullException("document"); TextPointer contentStart = document.ContentStart; // clear any existing highlight TextRange documentRange = new TextRange(document.ContentStart, document.ContentEnd); documentRange.ApplyPropertyValue(FlowDocument.BackgroundProperty, FlowDocument.BackgroundProperty.DefaultMetadata.DefaultValue); if (interval == null) return null; // highlight the new text int startOffset = interval.Start; int endOffset = interval.End; TextPointer highlightStart = document.GetPointerFromCharOffset(ref startOffset); TextPointer highlightStop = document.GetPointerFromCharOffset(ref endOffset); if (startOffset != 0 || endOffset != 0) return null; var textRange = new TextRange(highlightStart, highlightStop); textRange.ApplyPropertyValue(FlowDocument.BackgroundProperty, Brushes.Yellow); return textRange.Start; }