I'm trying to recolour all the numbers in a WPF RichTextBox
to be coloured differently. I've been following this tutorial, but I find that letters in the text are highlighted almost at random. This is the handler I have so far:
private void DescriptionText_TextChanged(object sender, TextChangedEventArgs e)
{
var range = new TextRange(DescriptionText.Document.ContentStart, DescriptionText.Document.ContentEnd);
var regex = new Regex("[0-9]+");
var num_ranges = new List<TextRange>();
// add all the ranges with numbers
foreach (Match match in num_reg.Matches(range.Text))
{
var start = range.Start.GetPositionAtOffset(match.Index);
var end = range.Start.GetPositionAtOffset(match.Index + match.Length);
num_ranges.Add(new TextRange(start, end));
}
// unsuscribe before making changes
DescriptionText.TextChanged -= this.DescriptionText_TextChanged;
range.ClearAllProperties();
range.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(Colors.Black));
foreach (var r in num_ranges)
{
r.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(Colors.Red));
}
DescriptionText.TextChanged += this.DescriptionText_TextChanged;
}