Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Textbox - horizontal text centering

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

like image 439
noname Avatar asked Aug 15 '10 00:08

noname


People also ask

How do I center text in a text box vertically in Word?

1 Select the text you want to center between the top and bottom margins. 2 On the Page Layout tab, click the Page Setup Dialog Box Launcher. 3 Select the Layout tab. 4 In the Vertical alignment box, click Center 5 In the Apply to box, click Selected text, and then click OK.

How do I center text in the middle of a box in Word?

On the Shape Format tab, click Format Pane. Click the Shape Options tab if it isn't already selected. , and then click Text Box. Choose Top, Middle, or Bottom from the Vertical alignment drop-down list.


8 Answers

Set the TextAlignment property to Center:

<TextBox Width="200"
         Text="Hello world !"
         TextAlignment="Center"/>
like image 114
Thomas Levesque Avatar answered Sep 25 '22 11:09

Thomas Levesque


HorizontalContentAlignment="Center" VerticalContentAlignment="Center"
like image 22
Tarık Özgün Güner Avatar answered Sep 26 '22 11:09

Tarık Özgün Güner


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>
like image 30
Ghotekar Rahul Avatar answered Sep 23 '22 11:09

Ghotekar Rahul


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

Try adding this two peoperties to your control

VerticalAlignment="Stretch" 
VerticalContentAlignment="Center"
like image 21
Othman Dahbi-Skali Avatar answered Sep 26 '22 11:09

Othman Dahbi-Skali


<TextBox Width="200" Text="Hello world !" VerticalAlignment="Center"/>
like image 41
Sonhja Avatar answered Sep 25 '22 11:09

Sonhja


<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

like image 27
Naveen S Avatar answered Sep 23 '22 11:09

Naveen S


VerticalContentAlignment sets the Alignment for the Text in a Textbox

like image 29
justme Avatar answered Sep 24 '22 11:09

justme


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"/>
like image 38
Harlan Avatar answered Sep 24 '22 11:09

Harlan