Search code examples
c#wpfpaddingtextblock

Remove the C# TextBlock inlines elements padding


About TextBlock UIElement in C#, we could add several Run object into it, which will be appended to inlines property. That's one way we show several pieces of text with different format(font, size and etc) in one TextBlock.

My question is: when I add, such as, two Run object into one TextBlock, there exist a padding between each Run object. For example, I add "12" and "34" Run objects, and finally they will be shown as "12 34" in the view. But what I need is they should be connected together as one word - "1234" - without that padding

Is there any setting we can use to prevent this padding?


Solution

  • Instead of

    <TextBlock>
        <Run Text="12"/>
        <Run Text="34"/>
    </TextBlock>        
    

    write the runs in one line like this

    <TextBlock>
        <Run Text="12"/><Run Text="34"/>
    </TextBlock>
    

    And the space will be gone.