Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Silverlight: TextBox VerticalContentAlignment="Center"

I'm trying to vertically center the content of a TextBox with the VerticalContentAlignment property but it seems to have no effect at all. The text stays at the top. Can anyone tell me how to do this?

Here's my code:

<TextBox Grid.Column="1"
     Grid.Row="0"
     Width="200"
     Height="28"
     VerticalAlignment="Center"
     VerticalContentAlignment="Center" />
like image 856
Anonymous Coward Avatar asked Jul 19 '11 08:07

Anonymous Coward


1 Answers

It is possible to make the TextBox center its text vertically. However, that does require you to reapply its ControlTemplate.

To do this:

  1. Copy the Style and the ControlTemplate from the TextBox Styles and Templates page on MSDN to a suitable <UserControl.Resources> element. (This ControlTemplate is actually for a validation tooltip; the ControlTemplate we'll change is within the Style.)
  2. Find the ScrollViewer element within the Style for the TextBox, and add a VerticalAlignment="Center" property to it.

Alternatively, you could add the property

VerticalAlignment="{TemplateBinding VerticalContentAlignment}"

to the ScrollViewer. This should allow you to set the vertical alignment of the contents of your TextBoxes using the VerticalContentAlignment property.

You can follow much the same approach if you wish to change the horizontal alignment of a TextBox's content as well.

like image 164
Luke Woodward Avatar answered Oct 01 '22 10:10

Luke Woodward