【发布时间】:2011-08-06 20:06:32
【问题描述】:
我正在尝试使用 RichTextBox,我的第一感觉是:“使用起来有什么复杂的!”... 太棒了...
所以我试图突出显示我的 RichTextBox 中包含的文本。
我目前有以下代码:
TextRange range = new TextRange(MyTextInput.Document.ContentStart, MyTextInput.Document.ContentEnd);
range.Text = @"TOP a multiline text or file END";
Regex reg = new Regex("(top|file|end)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
foreach (Match match in reg.Matches(range.Text))
{
TextPointer start = range.Start.GetPositionAtOffset(match.Index, LogicalDirection.Forward);
TextPointer end = range.Start.GetPositionAtOffset(match.Index + match.Length, LogicalDirection.Backward);
// text contains the exact match I want
string text = range.Text.Substring(match.Index, match.Length);
// here the highlighted text isn't the text I searched...
TextRange textrange = new TextRange(start, end);
textrange.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(Colors.Blue));
textrange.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
}
TOP 正确突出显示,但 file 或 end 未正确突出显示,但突出显示我 or。
有什么建议吗?
【问题讨论】:
标签: c# wpf richtextbox syntax-highlighting