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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:24:02  来源:igfitidea点击:

Vertical Align in WPF TextBox

wpftextboxfont-size

提问by u298834

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:

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

alt text

替代文字

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 Paddingproperties 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 Heightproperties, 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 Heightset to 40to these TextBoxcontrols.

您已为这些控件指定了显式Height设置。40TextBox

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 的文本无法适应给定的高度。所以,有几个解决方案

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