首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在WPF RichTextBox中突出显示数字

如何在WPF RichTextBox中突出显示数字
EN

Stack Overflow用户
提问于 2018-05-11 01:52:50
回答 2查看 284关注 0票数 2

我正在尝试重新着色一个WPF RichTextBox中的所有数字以不同的颜色。我一直在关注this tutorial,但我发现文本中的字母几乎是随机突出显示的。这是到目前为止我拥有的处理程序:

代码语言:javascript
复制
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;
}
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50278754

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档