noname
noname

Reputation: 591

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.

Upvotes: 59

Views: 83075

Answers (8)

Harlan
Harlan

Reputation: 148

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

Upvotes: 1

Othman Dahbi-Skali
Othman Dahbi-Skali

Reputation: 632

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

Try adding this two peoperties to your control

VerticalAlignment="Stretch" 
VerticalContentAlignment="Center"

Upvotes: 6

Naveen S
Naveen S

Reputation: 63

<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

Upvotes: 4

Ghotekar Rahul
Ghotekar Rahul

Reputation: 342

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>

Upvotes: 7

Thomas Levesque
Thomas Levesque

Reputation: 292695

Set the TextAlignment property to Center:

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

Upvotes: 81

justme
justme

Reputation: 47

VerticalContentAlignment sets the Alignment for the Text in a Textbox

Upvotes: 2

HorizontalContentAlignment="Center" VerticalContentAlignment="Center"

Upvotes: 66

Sonhja
Sonhja

Reputation: 8458

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

Upvotes: 3

Related Questions