Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vertical Align in WPF TextBox

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" />
like image 502
開発者 Avatar asked Dec 15 '10 17:12

開発者


People also ask

How do I center text in a WPF TextBox?

VerticalAlignment = "Center" and padding You can reach the text within a WPF-TextBox with the combination VerticalAlignment and Padding. Like VerticalAlignment = "Center" Padding = "5" Padding causes the text field to become larger and adapt to the surrounding element. Save this answer.

How do I center TextBlock WPF?

If you want to center each line, use a TextBlock instead, and set TextAlignment="Center" .

What is TextBlock WPF?

The TextBlock control provides flexible text support for UI scenarios that do not require more than one paragraph of text. It supports a number of properties that enable precise control of presentation, such as FontFamily, FontSize, FontWeight, TextEffects, and TextWrapping.

What is the difference between TextBlock and label in WPF?

Labels usually support single line text output while the TextBlock is intended for multiline text display. For example in wpf TextBlock has a property TextWrapping which enables multiline input; Label does not have this.


3 Answers

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

<TextBox Text="The text" Height="40" VerticalContentAlignment="Center" />
like image 146
Tal Segal Avatar answered Sep 18 '22 18:09

Tal Segal


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" />
like image 38
Donut Avatar answered Sep 20 '22 18:09

Donut


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" />
like image 24
decyclone Avatar answered Sep 19 '22 18:09

decyclone