10

How can I vertically align a Label and TextBlock at Top so that their first lines of text line up?

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition />
    </Grid.RowDefinitions>
    <Label Grid.Column="0" VerticalAlignment="Top">Some Label:</Label>
    <TextBlock Grid.Column="0" VerticalAlignment="Top">Some text<TextBlock>
</Grid>

The above code gives me this:

Vertically misaligned Label and TextBlock text http://img156.imageshack.us/img156/4940/labeltextblock.png

sourcenouveau
  • 29,356
  • 35
  • 146
  • 243
  • The question's code is quite broken. The Label and TextBlock are both in column 0, row 0, and appear on top of each other. The TextBlock "closing" tag is actually a 2nd TextBlock _opening_ tag so I'm guessing the code wasn't copied from a working example. (The image for this old question is also broken.) Tested 2021-07-17. – Andrew D. Bond Jul 17 '21 at 08:25
  • The edit queue is full, but corrected question code: ``` B ``` – Andrew D. Bond Jul 17 '21 at 08:36

3 Answers3

21

The extra space around the label comes from the Padding property. To remove the space, you can explicitly set the Padding property to "0" directly on the Label, or, of course, set it via a Style.

Daniel Pratt
  • 12,007
  • 2
  • 44
  • 61
1
<TextBlock>
<InlineUIContainer BaselineAlignment="Top"><Label Content="Label"/></InlineUIContainer>
<InlineUIContainer BaselineAlignment="Top"><TextBlock>TextBlock Content</TextBlock>                 </InlineUIContainer>
</TextBlock>

HTH.

HelloSam
  • 2,225
  • 1
  • 20
  • 21
0

Here is a workaround: Align bottoms of text in controls.

I posted a connection: https://connect.microsoft.com/WPF/feedback/ViewFeedback.aspx?FeedbackID=523432, please vote.

Community
  • 1
  • 1
Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632