Decide FontStyle (Bold, Italic, Underlined) for RichTextBox

隐身守侯 提交于 2019-12-02 00:18:18

Bold is a FontWeight. You can apply it directly.

As MSDN Doc states "Gets or sets the weight or thickness of the specified font."

You can either set it in xaml

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

or in codebehind:

richText.FontWeight = FontWeights.Bold;

If your trying to switch FontFamily that would be like:

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

or FontStyle:

richText.FontStyle = FontStyles.Italic;

Update: (for updating RichTextBox inline)

This is just a quick mock-up. Using this as an example. Please structure it for your requirements.

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);
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!