Iva*_*vic 27 c# wpf fonts textbox
有没有办法改变我希望放在TextBox或RichTextBox上的文本的某些部分的颜色和字体.我正在使用C#WPF.
例如
richTextBox.AppendText("Text1 " + word + " Text2 ");
Run Code Online (Sandbox Code Playgroud)
可变字例如是Text1和Text2中的其他颜色和字体.是否有可能以及如何做到这一点?
Gim*_*mno 45
如果你只是想做一些快速着色,使用RTB内容的结尾作为范围并应用格式可能是最简单的解决方案,例如
TextRange rangeOfText1 = new TextRange(richTextBox.Document.ContentEnd, richTextBox.Document.ContentEnd);
rangeOfText1.Text = "Text1 ";
rangeOfText1.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Blue);
rangeOfText1.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
TextRange rangeOfWord = new TextRange(richTextBox.Document.ContentEnd, richTextBox.Document.ContentEnd);
rangeOfWord.Text = "word ";
rangeOfWord.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Red);
rangeOfWord.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Regular);
TextRange rangeOfText2 = new TextRange(richTextBox.Document.ContentEnd, richTextBox.Document.ContentEnd);
rangeOfText2.Text = "Text2 ";
rangeOfText2.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Blue);
rangeOfText2.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
Run Code Online (Sandbox Code Playgroud)
如果您正在寻找更高级的解决方案,我建议您阅读有关FlowDocument的MSDN页面,因为这为您提供了格式化文本的极大灵活性.
Lea*_*ner 16
你可以尝试一下.
public TestWindow()
{
InitializeComponent();
this.paragraph = new Paragraph();
rich1.Document = new FlowDocument(paragraph);
var from = "user1";
var text = "chat message goes here";
paragraph.Inlines.Add(new Bold(new Run(from + ": "))
{
Foreground = Brushes.Red
});
paragraph.Inlines.Add(text);
paragraph.Inlines.Add(new LineBreak());
this.DataContext = this;
}
private Paragraph paragraph;
Run Code Online (Sandbox Code Playgroud)
因此,请使用RichTextBox的Document属性
归档时间: |
|
查看次数: |
79908 次 |
最近记录: |