我可以动态地将文本块中的部分文本加粗吗?

时间:2018-03-07 05:32:55

标签: c# .net uwp

例如,我想要一个绑定到observableCollection的文本块列表如下:

  1. A C
  2. CF

  3. OY

  4. B OOK

  5. 一个的 SOR的

  6. EFORE

1 个答案:

答案 0 :(得分:0)

您可以在C#中使用Flow document,使用FlowDocument.FontWeight

enter image description here

<FlowDocumentReader>
  <FlowDocument
    FontFamily="Century Gothic"
    FontSize="12"
    FontStretch="UltraExpanded"
    FontStyle="Italic"
    FontWeight="UltraBold"
  >
    <Paragraph>
      Any font settings on this paragraph would override the font settings
      for the FlowDocument.
    </Paragraph>
  </FlowDocument>
</FlowDocumentReader>

FlowDocument flowDoc = new FlowDocument(new Paragraph(new Run("A bit of text content...")));
// Set the desired column gap to 10 device independend pixels.
flowDoc.FontFamily = new FontFamily("Century Gothic");
flowDoc.FontSize = 12.0;
flowDoc.FontStretch = FontStretches.UltraExpanded;
flowDoc.FontStyle = FontStyles.Italic;
flowDoc.FontWeight = FontWeights.UltraBold;

另一种方法是使用运行标记,正如@Gaurang建议的那样:

public SectionExample()
{

    // Create three paragraphs
    Paragraph myParagraph1 = new Paragraph(new Run("Paragraph 1"));
    Paragraph myParagraph2 = new Paragraph(new Run("Paragraph 2"));
    Paragraph myParagraph3 = new Paragraph(new Run("Paragraph 3"));

    // Create a Section and add the three paragraphs to it.
    Section mySection = new Section();
    mySection.Background = Brushes.Red;

    mySection.Blocks.Add(myParagraph1);
    mySection.Blocks.Add(myParagraph2);
    mySection.Blocks.Add(myParagraph3);

    // Create a FlowDocument and add the section to it.
    FlowDocument myFlowDocument = new FlowDocument();
    myFlowDocument.Blocks.Add(mySection);

    this.Content = myFlowDocument;
}