Home > Software design >  RichtTextBlock is not displaying multiple spaces present in the text
RichtTextBlock is not displaying multiple spaces present in the text

Time:09-28

I'm trying to display some table data in RichTextBlock. The data contains several lines of text, and the text contains multiple spaces. I'm adding each line as Run objects to the Paragraph.

        string[] lines = tableData;
        Paragraph para = this.TextContent.Blocks.First() as Paragraph;
        para.Inlines.Add(new LineBreak());
        para.Inlines.Add(new LineBreak());
        para.Inlines.Add(new LineBreak());
        para.Inlines.Add(new LineBreak());

        for (int l = 0; l < lines.Length; l  )
        {

            Run r = new Run()
            {
                Text = lines[l],
                FontSize = 9,
            };
            para.Inlines.Add(r);
            para.Inlines.Add(new LineBreak());
        }

RichTextBlock is replacing multiple spaces and keeping one, as shown here RichTextBlock ouput Input text looks like this in Notepad (enabled special characters to highlight spaces) Actual input/output displayed in Notepad

I couldn't find any property in the Paragraph or Run classes to avoid this scenario.

What do I miss to get the RichTextBlock to display the exact text displayed in Notedpadd like this? Expected output

Thank you

CodePudding user response:

As per the comment above, this problem isn't caused by missing spaces. Notepad displays the table in a monospace font, where all characters have the same width, but the RichTextBlock displays the text using a proportional font, where characters can have different widths. Spaces seem to be narrower than other characters, hence the rows of the table don't line up.

CodePudding user response:

I HAVE A DOUBT IN MY PROFILE MY DOUBT IS ABOUT private void SelectionChanged(object sender, SelectionChangedEventArgs e)

  • Related