akonsu
akonsu

Reputation: 29538

How to prevent TextBox from stretching vertically inside Grid

In the markup below, the text box vertically to fills the entire grid row, no matter how big the row's height is. For a single line text box this does not look good. I need it to center vertically instead and have the height just enough to fit the current font. Setting the Height property on the text box helps but I do not want to hard code the height in case the font changes.

<Grid FocusManager.FocusedElement="{Binding ElementName=TitleBox}">
  <Grid.ColumnDefinitions>
    <ColumnDefinition Width="*" />
    <ColumnDefinition Width="Auto" />
  </Grid.ColumnDefinitions>
  <TextBox Name="TitleBox"
           Text="{Binding Title, UpdateSourceTrigger=PropertyChanged}"
           Grid.Column="0" />
  <Button Command="{Binding CreateCommand}"
          IsDefault="True"
          Grid.Column="1">Create</Button>
</Grid>

Upvotes: 0

Views: 4648

Answers (1)

Martin Liversage
Martin Liversage

Reputation: 106796

Set the VerticalAlignment of the text box to Center. The default is Stretch which explains why it stretches to fill the entire grid cell:

VerticalAlignment="Center"

Upvotes: 7

Related Questions