Decide FontStyle (Bold, Italic, Underlined) for RichTextBox

后端 未结 1 1104
时光说笑
时光说笑 2021-01-23 04:57

How exactly do you change the Font in a RichTextBox?

Looking around gives me old answers that doesn\'t seem to work any more. I thought it would be as simple as doing

相关标签:
1条回答
  • 2021-01-23 05:19

    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);
    }
    
    0 讨论(0)
提交回复
热议问题