Guilherme Campos
Guilherme Campos

Reputation: 379

How to fit textbox width with a set of columns in a grid?

Supose I have a grid with 4 columns and want to put a textbox in the second column and span it to the last column. How could I fit the textbox width to be as wide as the 3 last columns?

I have tried something with Borders, but it didn't work.

<Grid>
      <Grid.ColumnDefinitions>
           <ColumnDefinition Width="Auto" />
           <ColumnDefinition Width="Auto" />
           <ColumnDefinition Width="Auto" />
           <ColumnDefinition Width="Auto" />
      </Grid.ColumnDefinitions>

      <TextBox Grid.Column="1" Grid.ColumnSpan="3" />
</Grid>

Upvotes: 5

Views: 8296

Answers (3)

Maheep
Maheep

Reputation: 5605

As Dipesh Bhatt has rightly put in his answer that solution to your problem is to put * or something specific as width for columns.

What happens in your example is auto width for column says "take as much is required" hence it takes very little width, as empty textbox will require very very small width, which is hardly visible.

A * in width for column says "acquire whatever space is available" this will increase width of column hence it will give more space to textbox.

This means you have already achieved what you wanted but its hardly visible due to minimal width of columns.

Upvotes: 3

paparazzo
paparazzo

Reputation: 45096

Right now the last 3 columns are fitting the text box as the width is auto.

  <Grid>
  <Grid.ColumnDefinitions>
       <ColumnDefinition Width="*" />
       <ColumnDefinition Width="*" />
       <ColumnDefinition Width="*" />
       <ColumnDefinition Width="*" />
  </Grid.ColumnDefinitions>

  <TextBox Grid.Column="1" Grid.ColumnSpan="3" />
  </Grid>

Upvotes: 3

Dipesh Bhatt
Dipesh Bhatt

Reputation: 825

Give TextBox some width or make ColumnDefinition width as * instead of Auto. It is spanning till the last column but just you are not able to see it as the width is Auto.

Upvotes: 7

Related Questions