一个字符在文本框中的C#变色框中、字符、文本

由网友(精英↘战ING)分享简介:C# - WPF:我怎样才能改变一个字符在文本框中的颜色?例如:单词Hello中,H颜色变成红色C# - WPF :how can I change the color of just one character in a text box ? example : Word Hello, Color o...

C# - WPF: 我怎样才能改变一个字符在文本框中的颜色? 例如:单词Hello中,H颜色变成红色

C# - WPF : how can I change the color of just one character in a text box ? example : Word Hello, Color of H becomes Red

推荐答案

您不能用一个文本框做到这一点,但你可以使用一个RichTextBox: WPF的RichTextBox教程

You can not do this with a textbox, but you can use a richtextbox: WPF RichTextBox Tutorial

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));
阅读全文

相关推荐

最新文章