private static void ApplyHighlight(HighlightTextBlock textBlock) { string highlightPhrase = textBlock.HighlightPhrase; string text = textBlock.Text; if (string.IsNullOrEmpty(highlightPhrase)) { textBlock.Inlines.Clear(); textBlock.Inlines.Add(text); } else { int index = text.IndexOf(highlightPhrase, textBlock.IsCaseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase); textBlock.Inlines.Clear(); if (index < 0) { textBlock.Inlines.Add(text); } else { if (index > 0) { textBlock.Inlines.Add(text.Substring(0, index)); } textBlock.Inlines.Add(new Run(text.Substring(index, highlightPhrase.Length)) { Background = textBlock.HighlightBrush }); index += highlightPhrase.Length; if (index < text.Length) { textBlock.Inlines.Add(text.Substring(index)); } } } }