WPF:对齐标签及其文本框的基线 [英] WPF: Aligning the base line of a Label and its TextBox
问题描述
比方说,我在Label旁边有一个简单的TextBox:
Let's say I have a simple TextBox next to a Label:
<StackPanel>
<StackPanel Orientation="Horizontal">
<Label Margin="3">MyLabel</Label>
<TextBox Margin="3" Width="100">MyText</TextBox>
</StackPanel>
...
</StackPanel>
这将产生以下结果:
如您所见,MyLabel和MyText的基线不对齐,这看起来很丑.当然,我可以开始研究边距,直到它们匹配为止,但是由于这是一个普遍的要求,因此我确信WPF提供了更简单,更优雅的解决方案,我只是还没有发现...
As you can see, the base lines of MyLabel and MyText are not aligned, which looks ugly. Of course, I could start playing around with the margins until they match up, but since this is such a common requirement I'm sure that WPF provides a much easier and more elegant solution, which I just haven't found yet...
推荐答案
我认为,此行为是由于TextBox
默认为Stretch
的垂直对齐方式而导致的,从而导致其填充了可用的内容.空格,并在文本下方多出几个像素.如果您改用以下方式:
This behaviour is, I think, caused by the fact that the TextBox
defaults to a vertical alignment of Stretch
, which causes it to fill the available space and have the extra couple of pixels under the text. If you use this instead:
<StackPanel>
<StackPanel Orientation="Horizontal">
<Label >MyLabel</Label>
<TextBox VerticalAlignment="Center" Width="100">MyText</TextBox>
</StackPanel>
</StackPanel>
...您应该会看到更干净的结果.
... you should see a cleaner result.
这篇关于WPF:对齐标签及其文本框的基线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!