sourcenouveau
sourcenouveau

Reputation: 30524

Vertically aligning Labels and TextBlocks at Top in XAML

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

Upvotes: 10

Views: 8927

Answers (3)

HelloSam
HelloSam

Reputation: 2345

<TextBlock>
<InlineUIContainer BaselineAlignment="Top"><Label Content="Label"/></InlineUIContainer>
<InlineUIContainer BaselineAlignment="Top"><TextBlock>TextBlock Content</TextBlock>                 </InlineUIContainer>
</TextBlock>

HTH.

Upvotes: 1

Shimmy Weitzhandler
Shimmy Weitzhandler

Reputation: 104741

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.

Upvotes: 0

Daniel Pratt
Daniel Pratt

Reputation: 12077

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.

Upvotes: 21

Related Questions