如果您只想进行快速着色,则将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);
如果您正在寻找更高级的解决方案,建议阅读有关Flowdocument的MSDN页面,因为这为您设置文本格式提供了极大的灵活性。



