Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从文本中的给定位置获取字符的颜色?_C#_Wpf_Richtextbox - Fatal编程技术网

C# 如何从文本中的给定位置获取字符的颜色?

C# 如何从文本中的给定位置获取字符的颜色?,c#,wpf,richtextbox,C#,Wpf,Richtextbox,我主要想检查文本中给定位置的字符是否是某种颜色。比如说 if(character.inAposition == blue){ change characters color } 这就是我最初更改文本颜色的方式 TextPointer start = position.GetPositionAtOffset(indexInRun); TextPointer end = start.GetPositionAtOffset(word.Length); TextRange rangeText =

我主要想检查文本中给定位置的字符是否是某种颜色。比如说

if(character.inAposition == blue){
   change characters color
}
这就是我最初更改文本颜色的方式

TextPointer start = position.GetPositionAtOffset(indexInRun);
TextPointer end = start.GetPositionAtOffset(word.Length);
TextRange rangeText = new TextRange(start, end);
rangeText.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Blue);

其中indexInRun是与“word”匹配的任何子字符串的起始索引。

您不能检查字符。您可以选中RichTextBox Inlines集合的Run元素。但你必须检查它是否真的是一个运行或其他东西,比如一个完整的文本块或图像等等。

我只是在其中添加了一些代码,如果你能给我一个例子,说明你将如何做itTextRange基本上只是一个字符串,它甚至不从TextElement继承。您可以遍历Inlines属性并在运行对象(如果它们确实是运行对象)中查找单词。您可以为以后标记所有位置,以便更改其前景。如果找到了,但它是句子的一部分,您可以从该位置删除该句子,将其打断,然后分别用您的单词重新组合,这样您就可以更改其前景,然后将其放回同一位置。