我希望功能等同于支持多种颜色的文本块。
我已经尝试创建一个UserControl并添加多个文本块并将其前景设置为我需要的颜色,但这非常慢,因为我将每隔几秒更改和清除文本。
我也尝试过使用RichTextBox,但似乎它不是以编程方式填充的。
建议?
答案 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 });