wpf WPF文本框中的垂直对齐
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4453175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Vertical Align in WPF TextBox
提问by u298834
I have 2 TextBox
es in my wpf app, one for user name and other for password, both have FontSize=20
, but the text appears like this:
我的TextBox
wpf 应用程序中有 2 个es,一个用于用户名,另一个用于密码,两者都有FontSize=20
,但文本显示如下:
How can I fix this?
我怎样才能解决这个问题?
Xaml:
Xml:
<TextBox Grid.Row="1" Grid.Column="1" Height="40" BorderThickness="1" BorderBrush="#FFD5D5D5" FontSize="36" Text="test" />
<PasswordBox Grid.Row="3" Grid.Column="1" Height="40" BorderThickness="1" BorderBrush="#FFD5D5D5" FontSize="36" Password="test" />
采纳答案by Donut
Adjust the Padding
properties of these controls, e.g. Padding="0"
:
调整Padding
这些控件的属性,例如Padding="0"
:
<TextBox Grid.Row="1" Grid.Column="1" Height="40" BorderThickness="1" BorderBrush="#FFD5D5D5" FontSize="36" Text="test" Padding="0" />
<PasswordBox Grid.Row="3" Grid.Column="1" Height="40" BorderThickness="1" BorderBrush="#FFD5D5D5" FontSize="36" Password="test" Padding="0" />
Or, don't set the Height
properties, and instead let the controls size themselves automatically based on the height of their content:
或者,不要设置Height
属性,而是让控件根据其内容的高度自动调整大小:
<TextBox Grid.Row="1" Grid.Column="1" BorderThickness="1" BorderBrush="#FFD5D5D5" FontSize="36" Text="test" />
<PasswordBox Grid.Row="3" Grid.Column="1" BorderThickness="1" BorderBrush="#FFD5D5D5" FontSize="36" Password="test" />
回答by Tal Segal
To Center the text in a TextBox use the VerticalContentAlignment Property of the TextBox.
要使 TextBox 中的文本居中,请使用 TextBox 的 VerticalContentAlignment 属性。
<TextBox Text="The text" Height="40" VerticalContentAlignment="Center" />
回答by decyclone
You have given explicit Height
set to 40
to these TextBox
controls.
您已为这些控件指定了显式Height
设置。40
TextBox
Please remove it and let them take enough space to show their content.
请删除它,让他们有足够的空间来显示他们的内容。
<TextBox Grid.Row="1"
Grid.Column="1"
BorderThickness="1"
BorderBrush="#FFD5D5D5"
FontSize="36"
Text="test" />
<PasswordBox Grid.Row="3"
Grid.Column="1"
BorderThickness="1"
BorderBrush="#FFD5D5D5"
FontSize="36"
Password="test" />
回答by Zuhaib
The reason for this is because you have specified the FontSizeproperty as well as the Heightexplicitly. The text with the bigger FontSize cannot fit in the given height. So, there are a couple of solutions for this
这是因为您已经明确指定了FontSize属性和Height。具有较大 FontSize 的文本无法适应给定的高度。所以,有几个解决方案
- Increase the Height of the TextBox to 60 (but this will create a heighted TextBox which may not look good in the UI). Or, you can just skip Height property, so that it will automatically take the minimum space it needs.
- 将 TextBox 的高度增加到 60(但这会创建一个在 UI 中看起来不太好的高度 TextBox)。或者,您可以跳过 Height 属性,以便它自动占用所需的最小空间。
<TextBox Grid.Row="1"
Grid.Column="1"
BorderThickness="1"
BorderBrush="#FFD5D5D5"
FontSize="36"
Text="test" />
- Reduce FontSize, so that the text can fit in the TextBox with Height 40
- 减小 FontSize,使文本可以适应高度为 40 的 TextBox
<TextBox Grid.Row="1"
Grid.Column="1"
Height="40"
BorderThickness="1"
BorderBrush="#FFD5D5D5"
FontSize="24"
Text="test" />