我想使用不同颜色的WPF文本块显示文本的每一行。我有下面的代码,它使整个块的字体颜色为紫色,因为这是它设置的最后一种颜色。我怎样才能让每个药水以不同的颜色显示?C#WPF Textblock每行不同的字体颜色
private void btnShowPotions_Click(object sender, RoutedEventArgs e) {
tbPotionInfo.Foreground = Brushes.Green;
tbPotionInfo.Text = smallPotion.Name + "(" + smallPotion.AffectValue + ")\r\n";
tbPotionInfo.Foreground = Brushes.Blue;
tbPotionInfo.Text += mediumPotion.Name + "(" + mediumPotion.AffectValue + ")\r\n";
tbPotionInfo.Foreground = Brushes.Red;
tbPotionInfo.Text += largePotion.Name + "(" + largePotion.AffectValue + ")\r\n";
tbPotionInfo.Foreground = Brushes.Purple;
tbPotionInfo.Text += extremePotion.Name + "(" + extremePotion.AffectValue + ")\r\n";
}
,您可以利用Run'的'这里 – Gopichandar
看看此http://www.wpf-tutorial。 com/basic-controls/the-textblock-control-inline-formatting/- 特别是名为RUN和SPAN的部分。 –
使用richtextbox并将其设置为只读,或者您必须为每种颜色设置带有一个文本框的文本块 – Thorarins