Vertical Align in WPF TextBox

WpfTextboxFont Size

Wpf Problem Overview


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

Wpf Solutions


Solution 1 - Wpf

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

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

Solution 2 - Wpf

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

Solution 3 - Wpf

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

Solution 4 - Wpf

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

Solution 5 - Wpf

try this VerticalContentAlignment="Center" in your textbox

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Question開発者View Question on Stackoverflow
Solution 1 - WpfTal SegalView Answer on Stackoverflow
Solution 2 - WpfDonutView Answer on Stackoverflow
Solution 3 - WpfdecycloneView Answer on Stackoverflow
Solution 4 - WpfZuhaibView Answer on Stackoverflow
Solution 5 - WpfjojooojoView Answer on Stackoverflow