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" />
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.
If you want to center each line, use a TextBlock instead, and set TextAlignment="Center" .
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.
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.
To vertically center the text in a TextBox
use the VerticalContentAlignment
property:
<TextBox Text="The text" Height="40" VerticalContentAlignment="Center" />
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" />
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" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With