2

您究竟如何更改 RichTextBox 中的字体?

环顾四周给了我似乎不再起作用的旧答案。我认为这就像做richtextbox1.Font = Font.Bold;或类似的事情一样简单。原来不是,所以我环顾四周。显然您必须更改FontStylewhich is a readonly(??) 属性,但您必须创建一个新FontStyle对象。

但即使那样也行不通

你怎么做到这一点?编辑:

似乎不起作用:\

            rssTextBox.Document.Blocks.Clear();
            rssTextBox.FontWeight = FontWeights.Bold;
            rssTextBox.AppendText("Title: ");
            rssTextBox.FontWeight = FontWeights.Normal;
            rssTextBox.AppendText(rs.Title + "\n");
            rssTextBox.FontWeight = FontWeights.Bold;
            rssTextBox.AppendText("Publication Date: ");
            rssTextBox.FontWeight = FontWeights.Normal;
            rssTextBox.AppendText(rs.PublicationDate + "\n");
            rssTextBox.FontWeight = FontWeights.Bold;
            rssTextBox.AppendText("Description: ");
            rssTextBox.FontWeight = FontWeights.Normal;
            rssTextBox.AppendText(rs.Description + "\n\n");
4

1 回答 1

3

Bold是一个FontWeight。你可以直接申请。

正如MSDN Doc所述,“获取或设置指定字体的粗细或粗细”。

您可以在 xaml 中设置它

<RichTextBox FontWeight="Bold" x:Name="richText" />

或在代码隐藏中:

richText.FontWeight = FontWeights.Bold;

如果您尝试切换FontFamily,就像:

richText.FontFamily = new FontFamily("Arial");

FontStyle

richText.FontStyle = FontStyles.Italic;

更新:(用于更新RichTextBox内联)

这只是一个快速的模型。以此为例。请根据您的要求构建它。

richText.Document.Blocks.Clear();
Paragraph textParagraph = new Paragraph();
AddInLineBoldText("Title: ", ref textParagraph);
AddNormalTextWithBreak(rs.Title, ref textParagraph);
AddInLineBoldText("Publication Date: ", ref textParagraph);
AddNormalTextWithBreak(rs.PublicationDate, ref textParagraph);
AddInLineBoldText("Description: ", ref textParagraph);
AddNormalTextWithBreak(rs.Description, ref textParagraph);
AddNormalTextWithBreak("", ref textParagraph);
richText.Document.Blocks.Add(textParagraph);

private static void AddInLineBoldText(string text, ref Paragraph paragraph) {
  Bold myBold = new Bold();
  myBold.Inlines.Add(text);
  paragraph.Inlines.Add(myBold);
}

private static void AddNormalTextWithBreak(string text, ref Paragraph paragraph) {
  Run myRun = new Run {Text = text + Environment.NewLine};
  paragraph.Inlines.Add(myRun);
}
于 2013-04-20T22:46:27.253 回答