Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 253
Guilherme Campos Avatar asked Feb 28 '12 13:02

Guilherme Campos


3 Answers

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.

like image 194
Dipesh Bhatt Avatar answered Sep 17 '22 21:09

Dipesh Bhatt


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.

like image 23
Maheep Avatar answered Sep 19 '22 21:09

Maheep


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>
like image 31
paparazzo Avatar answered Sep 18 '22 21:09

paparazzo