如何使文本框宽度与网格中的一组列相匹配?

时间:2022-05-03 20:24:08

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?

我有一个有4列的网格,并希望在第二列中放置一个文本框并将其跨越到最后一列。我怎样才能使文本框宽度与最后3列一样宽?

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

我尝试过Borders的东西,但它没有用。

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

3 个解决方案

#1


7  

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.

给TextBox一些宽度或使ColumnDefinition宽度为*而不是Auto。它一直跨到最后一列,但由于宽度为自动,您无法看到它。

#2


3  

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

现在,最后3列适合文本框,因为宽度是自动的。

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

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

#3


2  

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

正如Dipesh Bhatt正确地提出的那样,解决问题的方法是将*或特定内容作为列的宽度。

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.

这意味着您已经达到了您想要的效果,但由于最小的列宽度,它几乎不可见。

#1


7  

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.

给TextBox一些宽度或使ColumnDefinition宽度为*而不是Auto。它一直跨到最后一列,但由于宽度为自动,您无法看到它。

#2


3  

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

现在,最后3列适合文本框,因为宽度是自动的。

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

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

#3


2  

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

正如Dipesh Bhatt正确地提出的那样,解决问题的方法是将*或特定内容作为列的宽度。

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.

这意味着您已经达到了您想要的效果,但由于最小的列宽度,它几乎不可见。