在 TextRange 中选择单词并为每个单词着色不同的 c# wpf 流文档

问题描述 投票:0回答:0

我有一个文本范围,由几个不同的字符串组成,用空格分隔。

我希望当我点击它们所在的行时,每个字符串的颜色都会改变。我设法编写了下面的代码以从插入符号位置提取行。我能够使用 ApplyPropertyValue() 方法来设置整个文本的颜色。但我不确定如何选择每个单词并单独设置它的颜色。

在此方面的任何帮助表示赞赏

//This is a sample string of the data I have:
//string s = "3   139*|CC|   6   33.9*|CC|"
//the substrings 3    139*|CC|     6      33.9*|CC| all need to be in different colours inside the document

TextPointer caretPos = spyFileTextBox.CaretPosition;
TextPointer start = caretPos.GetLineStartPosition(0);
TextPointer end = (caretPos.GetLineStartPosition(1) != null ? caretPos.GetLineStartPosition(1) : caretPos.DocumentEnd);
TextRange tr = new TextRange(start, end);

tr.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Green);
c# wpf xaml mvvm flowdocument
© www.soinside.com 2019 - 2024. All rights reserved.