[英]How to highlight numbers in WPF RichTextBox
我正在嘗試重新着色WPF RichTextBox
中的所有數字,以使其具有不同的顏色。 我一直在遵循本教程 ,但是我發現文本中的字母幾乎是隨機突出顯示的。 這是我到目前為止的處理程序:
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;
}
嘗試這個
DescriptionText.TextChanged -= this.DescriptionText_TextChanged;
var regExp = new Regex(@"^-*[0-9,\.]+$");
foreach (Match match in regExp.Matches(rtb.Text))
{
var textRange = rtb.Selection;
textRange.Select(match.Index, match.Length);
textRange.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(Colors.Red));
//rtb.SelectionColor = Color.Red;
}
DescriptionText.TextChanged += this.DescriptionText_TextChanged;
在修改了注釋中鏈接的問題之后,我進行了以下工作:(我在后面給出了相關代碼)
public class Tag
{
public TextPointer StartPosition;
public TextPointer EndPosition;
}
private void DescriptionText_TextChanged(object sender, TextChangedEventArgs e)
{
string text;
DescriptionText.TextChanged -= this.DescriptionText_TextChanged;
var range = new TextRange(DescriptionText.Document.ContentStart, DescriptionText.Document.ContentEnd);
range.ClearAllProperties();
var tags = new List<Tag>();
TextPointer navigator = DescriptionText.Document.ContentStart;
while (navigator.CompareTo(DescriptionText.Document.ContentEnd) < 0)
{
TextPointerContext context = navigator.GetPointerContext(LogicalDirection.Backward);
if (context == TextPointerContext.ElementStart && navigator.Parent is Run)
{
text = ((Run)navigator.Parent).Text;
if (text != "")
tags.AddRange(CheckWordsInRun(text, (Run)navigator.Parent));
}
navigator = navigator.GetNextContextPosition(LogicalDirection.Forward);
}
foreach (Tag tag in tags)
{
var r = new TextRange(tag.StartPosition, tag.EndPosition);
r.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(Colors.Red));
}
DescriptionText.TextChanged += this.DescriptionText_TextChanged;
}
private List<Tag> CheckWordsInRun(string text, Run theRun)
{
List<Tag> m_tags = new List<Tag>();
for (int i = 0; i < text.Length; i++)
{
if (Char.IsNumber(text[i]))
{
Tag t = new Tag();
t.StartPosition = theRun.ContentStart.GetPositionAtOffset(i, LogicalDirection.Forward);
t.EndPosition = theRun.ContentStart.GetPositionAtOffset(i + 1, LogicalDirection.Backward);
m_tags.Add(t);
}
}
return m_tags;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.