【问题标题】:WPF. How align two labels has different size on baseline in xamlWPF。如何在 xaml 中对齐两个标签在基线上的大小不同
【发布时间】:2017-08-20 08:31:43
【问题描述】:

我无法对齐两个大小不同且相邻的标签。我必须将变量显示为大尺寸,将单位显示为小。但由于字体基线的位置不同,文本不在同一行。看看我的 xaml:

<StackPanel VerticalAlignment="Bottom" Orientation="Horizontal">
        <Label Content="5" FontSize="70" FontWeight="DemiBold" Foreground="White" Padding="0" Margin="0" BorderBrush="White" BorderThickness="1" />
        <Label Content="s" FontSize="14" Foreground="White" Padding="0" Margin="0" BorderBrush="White" BorderThickness="1" VerticalAlignment="Bottom" />
    </StackPanel>

我看到了这个问题的解决方案:我可以选择边距或填充,但这不是一个好的决定。此外,我无法删除较大文本的上部间隙。可能存在更优雅的方式来解决这个问题。请告诉我这个,如果有人知道或告诉我这是不可能的,我会选择边距或填充。我一直在寻找解决方案。这里最相似:WPF: Aligning the base line of a Label and its TextBox 但是我的元素有不同的大小,这种方式对我不利。

感谢您的帮助!

【问题讨论】:

    标签: wpf xaml


    【解决方案1】:

    您应该使用该边距:

    <StackPanel VerticalAlignment="Bottom" Orientation="Horizontal">
                    <Label Content="5" FontSize="70" FontWeight="DemiBold" Foreground="White" Padding="0" Margin="0" BorderBrush="White" BorderThickness="1" />
                    <Label Content="s" FontSize="14" Foreground="White" Padding="0" Margin="0 0 0 12" BorderBrush="White" BorderThickness="1" VerticalAlignment="Bottom" />
                </StackPanel>
    

    【讨论】:

      【解决方案2】:

      如果您不需要使用Labels,您可以通过在TextBlock 中使用Runs 来实现基线对齐。

      <TextBlock VerticalAlignment="Bottom">
          <Run Text="5" FontSize="70" FontWeight="DemiBold" Foreground="White"  />
          <Run Text="s" FontSize="14" Foreground="White"  />
      </TextBlock>
      

      【讨论】:

      • 非常感谢您,filhit!你的解决方案帮助了我!
      猜你喜欢
      • 1970-01-01
      • 2015-09-10
      • 2018-07-04
      • 1970-01-01
      • 2015-06-23
      • 1970-01-01
      • 2015-08-07
      • 1970-01-01
      • 2012-04-12
      相关资源
      最近更新 更多