WPF 文本框中的垂直对齐
我的 wpf 应用程序中有 2 个 TextBox,一个用于用户名,另一个用于密码,两者都有 FontSize=20
,但文本显示如下:
我该如何解决这个问题?
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" />
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:
How can I fix this?
Xaml:
<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" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
要使文本在
TextBox
中垂直居中,请使用VerticalContentAlignment
属性:To vertically center the text in a
TextBox
use theVerticalContentAlignment
property:调整这些控件的
Padding
属性,例如Padding="0"
:或者,不要设置
Height
属性,而是让根据内容的高度自动控制自身大小:Adjust the
Padding
properties of these controls, e.g.Padding="0"
:Or, don't set the
Height
properties, and instead let the controls size themselves automatically based on the height of their content:您已将这些
TextBox
控件的显式Height
设置为40
。请删除它,让他们有足够的空间来展示他们的内容。
You have given explicit
Height
set to40
to theseTextBox
controls.Please remove it and let them take enough space to show their content.
原因是您已明确指定 FontSize 属性以及 Height。 FontSize 较大的文本无法适合给定的高度。
因此,有几种解决方案
The reason for this is because you have specified the FontSize property as well as the Height explicitly. The text with the bigger FontSize cannot fit in the given height.
So, there are a couple of solutions for this
在文本框中尝试这个
VerticalContentAlignment="Center"
try this
VerticalContentAlignment="Center"
in your textbox