WPF 文本框中的垂直对齐

发布于 2024-10-08 06:22:23 字数 481 浏览 9 评论 0原文

我的 wpf 应用程序中有 2 个 TextBox,一个用于用户名,另一个用于密码,两者都有 FontSize=20,但文本显示如下:

alt text

我该如何解决这个问题?

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 TextBoxes in my wpf app, one for user name and other for password, both have FontSize=20, but the text appears like this:

alt text

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

尝蛊 2024-10-15 06:22:23

要使文本在 TextBox 中垂直居中,请使用 VerticalContentAlignment 属性:

<TextBox Text="The text" Height="40" VerticalContentAlignment="Center" />

To vertically center the text in a TextBox use the VerticalContentAlignment property:

<TextBox Text="The text" Height="40" VerticalContentAlignment="Center" />
遗心遗梦遗幸福 2024-10-15 06:22:23

调整这些控件的 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" />

或者,不要设置 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" />

Adjust the Padding properties of these controls, e.g. 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:

<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" />
请持续率性 2024-10-15 06:22:23

您已将这些 TextBox 控件的显式 Height 设置为 40

请删除它,让他们有足够的空间来展示他们的内容。

<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" />

You have given explicit Height set to 40 to these TextBox controls.

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" />
迷雾森÷林ヴ 2024-10-15 06:22:23

原因是您已明确指定 FontSize 属性以及 Height。 FontSize 较大的文本无法适合给定的高度。
因此,有几种解决方案

  1. 可以将 TextBox 的高度增加到 60(但这会创建一个加高的 TextBox,在 UI 中看起来可能不太好)。或者,您可以跳过 Height 属性,这样它将自动占用所需的最小空间。
    <TextBox Grid.Row="1"
             Grid.Column="1"
             BorderThickness="1" 
             BorderBrush="#FFD5D5D5" 
             FontSize="36" 
             Text="test" />
  1. 减小 FontSize,使文本能够适合高度为 40 的 TextBox
    <TextBox Grid.Row="1"
             Grid.Column="1"
             Height="40"
             BorderThickness="1" 
             BorderBrush="#FFD5D5D5" 
             FontSize="24" 
             Text="test" />

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

  1. 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 Grid.Row="1"
             Grid.Column="1"
             BorderThickness="1" 
             BorderBrush="#FFD5D5D5" 
             FontSize="36" 
             Text="test" />
  1. Reduce FontSize, so that the text can fit in the TextBox with Height 40
    <TextBox Grid.Row="1"
             Grid.Column="1"
             Height="40"
             BorderThickness="1" 
             BorderBrush="#FFD5D5D5" 
             FontSize="24" 
             Text="test" />
天涯沦落人 2024-10-15 06:22:23

在文本框中尝试这个 VerticalContentAlignment="Center"

try this VerticalContentAlignment="Center" in your textbox

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文