Shashanka Kulamarva
Shashanka Kulamarva

Reputation: 3

Empty string inside a WPF TextBlock Run tag takes additional left Margin

This is the code that I am using and as an output I am getting an additional left margin in the 1st line as compared to the 2nd line(Very less though! But doesn't look good!). I mean "Test" has more left margin compared to "Test 2nd line". I know that it's because of empty string value assigned to the Run Text. Is there any way to remove this additional margin provided? I have to use two Run blocks in my code since I want it to be oriented horizontally and 2nd one should be Wrapped to next line when necessary!(Thus putting it inside stackpanel will not work!). And 1st Run Text value will not be empty always in my case. It's working fine when it's not empty. Is there any way not to have this additional left margin?

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>
    <TextBlock Grid.Row="0">
        <Run Text="" />
        <Run Text="Test" />
    </TextBlock>
    <TextBlock Grid.Row="1" Text="Test 2nd line" />
</Grid>

Upvotes: 0

Views: 843

Answers (2)

Ahmed Abdelkarim
Ahmed Abdelkarim

Reputation: 277

This is a XAML trick, this code will work exactly as you want.

<TextBlock Grid.Row="0">
    <Run Text="" /><Run Text="Test" />
</TextBlock>

Adding Run in a new line always lefts a white space. But putting them this way will NOT.

Upvotes: 0

Dean Chalk
Dean Chalk

Reputation: 20451

Try putting the Run blocks on the same line

<TextBlock Grid.Row="0">
    <Run Text="" /><Run Text="Test" />
</TextBlock>

this is because the TextBlock includes the new line between runs as a space

Upvotes: 2

Related Questions