private static void OnTextChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
TextBlock textBlock = d as TextBlock;
if (textBlock == null)
{
return;
}
string text = GetText(textBlock);
if (string.IsNullOrEmpty(text))
{
return;
}
Brush brush = GetHighlightBrush(textBlock) ?? new SolidColorBrush(Colors.Red);
int lastIndex = 0;
int index = text.IndexOf("<b>", 0, StringComparison.CurrentCulture);
textBlock.Inlines.Clear();
while (index >= 0)
{
textBlock.Inlines.Add(new Run { Text = text.Substring(lastIndex, index - lastIndex) });
int endIndex = text.IndexOf("</b>", index + 3, StringComparison.CurrentCulture);
textBlock.Inlines.Add(new Run { Text = text.Substring(index + 3, endIndex - index - 3), Foreground = brush });
lastIndex = endIndex + 4;
index = text.IndexOf("<b>", lastIndex, StringComparison.CurrentCulture);
}
textBlock.Inlines.Add(new Run { Text = text.Substring(lastIndex) });
}