WPF标签不垂直对齐到网格的底部

时间:2014-03-14 15:15:56

标签: wpf grid label vertical-alignment

我有一个Grid,有两列和一行,因此制作了两个单元格。每个单元格包含一个标签。一个标签具有默认字体大小,而另一个标签设置为20,并且两者都将其“垂直对齐”属性设置为“底部”但是它们位于的位置之间存在两个像素差异(如下面我可爱的图像所示)。

Example of Pixel difference

现在这是WPF的预期和工作的一部分吗?正如我所料,他们都坐在同一条线上。我目前可以想到的唯一方法是摆弄边际值。

    <Grid>
      <Grid.ColumnDefinitions>
          <ColumnDefinition Width="75"/>
          <ColumnDefinition Width="125"/>
      </Grid.ColumnDefinitions>
      <Grid.RowDefinitions>
          <RowDefinition/>
      </Grid.RowDefinitions>
      <Label Grid.Column="0" Grid.Row="0" Content="Notes" FontSize="20"  VerticalAlignment="Bottom"/>
      <Label Grid.Column="1" Grid.Row="0" Content="(Max something 1,00)" VerticalAlignment="Bottom"/>
    </Grid>

已回复谢里丹的回答是正确的。虽然我没有使用他的确切实现,但他的一个links给出了与我的代码相关的答案并遵循相同的原则。对于正在尝试此操作的其他人,我已将下面的代码包含在内。

        <Grid>
          <Grid.ColumnDefinitions>
            <ColumnDefinition Width="75"/>
            <ColumnDefinition Width="125"/>
          </Grid.ColumnDefinitions>
          <Grid.RowDefinitions>
            <RowDefinition/>
          </Grid.RowDefinitions>
          <TextBlock Grid.Column="0" Grid.Row="0" FontSize="20" LineHeight="44" LineStackingStrategy="BlockLineHeight" VerticalAlignment="Bottom">Notes</TextBlock>
          <TextBlock Grid.Column="1" Grid.Row="0" LineHeight="44" LineStackingStrategy="BlockLineHeight" VerticalAlignment="Bottom">(something else 1,000)   </TextBlock>
        </Grid>

enter image description here

2 个答案:

答案 0 :(得分:1)

这个问题有很多答案可以在网上找到。一个例子是Stack Overflow上的Align bottoms of text in controls帖子。但是,大多数答案(如同一个)表明您必须编写代码来解决此问题。我不同意他们的意见。您可以使用Inline类的BaselineAlignment property为您执行此操作。尝试这样的事情:

<TextBlock>
    <Span FontSize="40">Notes</Span>
    <Span BaselineAlignment="Baseline">(Max something 1,00)</Span>
</TextBlock>

enter image description here

答案 1 :(得分:0)

由于字体的字体+大小,出现“问题”。

实际发生的情况是:您的标签在该“框”中具有特定边框(大小)且字体居中。垂直对齐将设置在底部,因此不会修改正确的文本

Here you can see the difference of your Labels size