简体   繁体   English

在RichTextBox WPF中设置文本的垂直对齐方式时出现问题

[英]Issue in setting the vertical alignment for the text in RichTextBox WPF

In RichTextBox WPF, there is an issue while setting the vertical alignment for the content. 在RichTextBox WPF中,设置内容的垂直对齐方式时会出现问题。 I have tried setting Vertical Content alignment and TextAlignment for the document, but still it does not work. 我尝试为文档设置“垂直内容对齐方式”和“ TextAlignment”,但仍然无法正常工作。 The vertical alignment is still aligned at the top position and not at the center position. 垂直对齐仍然在顶部位置而不是在中心位置。

Please find the code example for reference of setting alignment, 请找到代码示例,作为设置对齐方式的参考,

        FlowDocument mcFlowDoc = new FlowDocument();
        // Create a paragraph with text  
        Paragraph para = new Paragraph();
        para.Inlines.Add(new Run("Flow Document\n"));
        para.Inlines.Add(new Bold(new Run("Content is not aligned\n.")));
        para.Inlines.Add(new Run("Vertical content alignment does not work in RichTextBox\n"));

        // Add the paragraph to blocks of paragraph  
        mcFlowDoc.Blocks.Add(para);

        // Set contents  
        richtextbox.Document = mcFlowDoc;

        //Set Alignment,


        richtextbox.VerticalContentAlignment = VerticalAlignment.Center;
        richtextbox.HorizontalContentAlignment = HorizontalAlignment.Center;
        richtextboxt.Document.TextAlignment = TextAlignment.Center;

Anyone interested, can you please suggest idea on this? 任何有兴趣的人,请问您对此有何建议?

You can try to set alignment for FlowDocument instead of RichTextBox. 您可以尝试为FlowDocument而不是RichTextBox设置对齐方式。
Take a look at the code below. 看一下下面的代码。

    FlowDocument mcFlowDoc = new FlowDocument();

    // Set alignment
    mcFlowDoc.TextAlignment = TextAlignment.Center;

    // Create a paragraph with text  
    Paragraph para = new Paragraph();
    para.Inlines.Add(new Run("Flow Document\n"));
    para.Inlines.Add(new Bold(new Run("Content is not aligned\n.")));
    para.Inlines.Add(new Run("Vertical content alignment does not work in RichTextBox\n"));

    // Add the paragraph to blocks of paragraph  
    mcFlowDoc.Blocks.Add(para);

    // Set contents  
    richTextBox.Document = mcFlowDoc;

You can also set Padding property on RichTextBox control as Kevin Pan mentioned here 您还可以像在这里提到的那样,在RichTextBox控件上设置Padding属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM