要以编程方式填充的彩色文本块

时间:2013-05-26 21:30:34

标签: wpf textarea

我希望功能等同于支持多种颜色的文本块。

我已经尝试创建一个UserControl并添加多个文本块并将其前景设置为我需要的颜色,但这非常慢,因为我将每隔几秒更改和清除文本。

我也尝试过使用RichTextBox,但似乎它不是以编程方式填充的。

建议?

1 个答案:

答案 0 :(得分:7)

TextBlock可以显示多个“运行” - 具有相同格式的文本块。使用Inlines属性访问它们:

using System.Windows.Documents;
using System.Windows.Media;

var inlines = textBlock.Inlines;
inlines.Add(new Run("This is red") { Foreground = Brushes.Red });
inlines.Add(new LineBreak()); // in case if you want new line
inlines.Add(new Run("And this is blue") { Foreground = Brushes.Blue });