您如何准确更改RichTextBox中的Font?

环顾四周可为我提供似乎不再有用的旧答案。我认为这样做就像richtextbox1.Font = Font.Bold;一样简单。原来不是,所以我环顾四周。显然,您必须更改作为FontStyle(??)属性的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");

最佳答案

BoldFontWeight。您可以直接应用它。

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);
}

关于c# - 确定RichTextBox的FontStyle(粗体,斜体,带下划线),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16125870/

10-11 19:40
查看更多