wpf C#更改文本框中一个字符的颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20102320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# Change color of one character in a text box
提问by daniyalahmad
C# - WPF : how can I change the color of just one character in a text box ? example : Word Hello, Color of H becomes Red
C#-WPF:如何仅更改文本框中一个字符的颜色?示例:Word Hello,H 的颜色变为红色
回答by user1567896
You can not do this with a textbox, but you can use a richtextbox: WPF RichTextBox Tutorial
您不能使用文本框执行此操作,但可以使用富文本框: WPF RichTextBox 教程
var textRange = MyRichTextBox.Selection;
var start = MyRichTextBox.Document.ContentStart;
var startPos = start.GetPositionAtOffset(0);
var endPos = start.GetPositionAtOffset(1);
textRange.Select(startPos, endPos);
textRange.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(Colors.Blue));
回答by mgokhanbakal
You can use richtexbox like below: You can even change backcolor for a particular character as well
您可以像下面这样使用richtexbox:您甚至可以更改特定字符的背景颜色
richTextBox1.SelectionStart = characterStartIndex;
richTextBox1.SelectionLength = 1;
richTextBox1.SelectionColor = Color.Red;
richTextBox1.SelectionBackColor = Color.Yellow;

