Anonymous Coward
Anonymous Coward

Reputation: 886

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

Upvotes: 7

Views: 5312

Answers (2)

Luke Woodward
Luke Woodward

Reputation: 64959

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.

Upvotes: 7

emp
emp

Reputation: 5065

The XAML code is correct, the following should be sufficient:

<TextBlock Text="Centered Text" VerticalAlignment="Center" />

Can you try that code outside your grid?

Check the attributes you defined on your Grid, this will probably cause the behaviour you have. Can you post the complete XAML code?

Upvotes: -1

Related Questions