Vertically aligning Labels and TextBlocks at Top in XAML

柔情痞子 提交于 2019-12-04 20:56:45

问题


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


回答1:


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.




回答2:


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

HTH.




回答3:


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.



来源:https://stackoverflow.com/questions/1099754/vertically-aligning-labels-and-textblocks-at-top-in-xaml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!