Textbox - horizontal text centering

WpfTextTextboxCentering

Wpf Problem Overview


Is there any simple way to center a text in textbox? I was looking for some built-in functions, but I found nothing.

Wpf Solutions


Solution 1 - Wpf

Set the TextAlignment property to Center:

<TextBox Width="200"
         Text="Hello world !"
         TextAlignment="Center"/>

Solution 2 - Wpf

HorizontalContentAlignment="Center" VerticalContentAlignment="Center"

Solution 3 - Wpf

You can reach the text within a WPF-TextBox with the combination VerticalAlignment and VerticalContentAlignment. You set the content to center and the total height with Stretch to the size of the comprehensive element like a grid row

<TextBox VerticalAlignment="Stretch" VerticalContentAlignment="Center"> 
Test 
</TextBox>

Solution 4 - Wpf

it's too late but this may be helpful for someone

Try adding this two peoperties to your control

VerticalAlignment="Stretch" 
VerticalContentAlignment="Center"

Solution 5 - Wpf

<TextBox Width="200" Text="Hello world !" VerticalAlignment="Center"/>

Solution 6 - Wpf

<TextBox VerticalAlignment="Center" Padding="5" > 

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.

The Image Shows a Output

Solution 7 - Wpf

VerticalContentAlignment sets the Alignment for the Text in a Textbox

Solution 8 - Wpf

If you are using a custom ControlTemplate, you need to change the ScrollViewer (x:Name="PART_ContentHost") to have VerticalAlignment="Center". (In addition to setting VerticalAlignment and VerticalContentAlignment on the TextBox itself as described in other answers.)

<ScrollViewer x:Name="PART_ContentHost" Focusable="false" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden" VerticalAlignment="Center"/>

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
QuestionnonameView Question on Stackoverflow
Solution 1 - WpfThomas LevesqueView Answer on Stackoverflow
Solution 2 - WpfTarık Özgün GünerView Answer on Stackoverflow
Solution 3 - WpfGhotekar RahulView Answer on Stackoverflow
Solution 4 - WpfOthman Dahbi-SkaliView Answer on Stackoverflow
Solution 5 - WpfSonhjaView Answer on Stackoverflow
Solution 6 - WpfNaveen S View Answer on Stackoverflow
Solution 7 - WpfjustmeView Answer on Stackoverflow
Solution 8 - WpfHarlanView Answer on Stackoverflow