locked
Preserving whitespace in Run element

    Question

  • When formatting a paragraph as so -

    <RichTextBlock>

    <Paragraph xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'> <Run></Run> <InlineUIContainer><TextBlock >Words here</TextBlock></InlineUIContainer> <Run xml:space="preserve"> </Run> <InlineUIContainer><TextBlock>More words here</TextBlock></InlineUIContainer> <Run xml:space="preserve"> Lastly some words here</Run> </Paragraph> </RichTextBlock>

    The <Run> element containing a single space is never displayed and ignore the space=preserve property. However the last run with a leading space is displayed correctly.

    How can I get a run containing a single space to display ?

    Tuesday, December 30, 2014 2:09 PM

All replies

  • Here is the workaround:

    <Run xml:space="preserve" Text="     " /> 
    Happy New Year!

    Tuesday, December 30, 2014 4:20 PM
  • Thanks for the answer but this still has the exact same issue.
    Tuesday, December 30, 2014 5:05 PM
  • Check this.  I can really reproduce the problem. But this is maybe because I am using a Silverlight project for Windows Phone.

    <TextBlock
           TextWrapping="Wrap" >
           <Run>This is a simple sample.</Run>
           <LineBreak/>
           <Run xml:space="preserve">    </Run>   <!-- this line has no effect -->
           <Run xml:space="preserve">You can put any content you want here...</Run>
           <LineBreak/>
           <Run xml:space="preserve" Text="     " />  <!-- this line is OK -->
           <Run xml:space="preserve">You can put any content you want here...</Run>                        
    </TextBlock>

    Tuesday, December 30, 2014 7:47 PM
  • After some experimentation it seems like it is only an issue when the run is contained between two InlineUIContainers. Any ideas why? or a workaround?

    I.e

    <RichTextBlock TextWrapping="Wrap" HorizontalAlignment="Center" >
    <!-- Not working -->
                <Paragraph xml:space="preserve"><InlineUIContainer><TextBlock>first part</TextBlock></InlineUIContainer><Run xml:space="preserve" Text="     " /><InlineUIContainer><TextBlock>second part</TextBlock></InlineUIContainer></Paragraph>
    <!-- working -->
                <Paragraph xml:space="preserve"><Run>First part</Run><Run xml:space="preserve" Text="     " /><InlineUIContainer><TextBlock>second part</TextBlock></InlineUIContainer></Paragraph>
    </RichTextBlock>

    Tuesday, December 30, 2014 8:17 PM