Search code examples
textwin-universal-apptextblockword-wrap

UWP text wrapping with TextBlock Run


My goal is to display a large number and highlight variably the first few numbers in a different color. Since RichTextBox would be overkill I found out about 'TextBlock' with 'Run' inside of them.

<TextBlock Style="{StaticResource SubText}" Margin="20" TextWrapping="Wrap" >
                     <Run x:Name="txtBlckNumberFirst" Foreground="#1ba1e2" Text="123456789123456789123456789" />
                     <Run x:Name="txtBlckNumberSecond" Text="123456789123456789123456789"  />

Inside the XAML preview this wraps just fine. At runtime though, it will always line break after the first 'Run'. I tried binding as well with the same result. Is this normal behaviour and is there an easy way to fix this?


Solution

  • If you put your Runs on separate lines a single space will be added after the first run. This single space does not show in the designer but applies at runtime (stupid, I know). So just put both Runs on the same line in your XAML.