[英]Autosizing a grid column to take up remaining space in parent
In WPF, I am having a heck of a time trying to get a grid to size properly. 在WPF中,我有一段时间试图让网格大小合适。
I have the following layout for my grid: 我的网格有以下布局:
<ItemsControl HorizontalContentAlignment="Stretch">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="80"/>
<ColumnDefinition Width="80"/>
<ColumnDefinition Width="100"/>
</Grid.ColumnDefinitions>
<Label Grid.Column="0" />
<Label Grid.Column="1"/>
<TextBox Grid.Column="2"/>
<Button Grid.Column="3"/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
The problem is that Width="Auto" seems to be sizing that column to the width of the content, and not filling out the extra space in the parent container. 问题是Width =“Auto”似乎是将该列调整为内容的宽度,而不是填充父容器中的额外空间。 This leaves the rest of the columns all unaligned, and ugly blank space at the end of each row. 这使得其余列全部未对齐,并且每行末尾都有丑陋的空白区域。
I'm probably missing something simple, but I can't seem to find a method to fit the column appropriately. 我可能错过了一些简单的东西,但我似乎无法找到适当的方法来适应列。
Or is there a better control for the job? 或者是否有更好的控制工作?
*
means fill or share. *
表示填写或分享。 If you had two with *
then they would share the width evenly. 如果你有两个*
那么他们将均匀地分享宽度。
<ColumnDefinition Width="*"/>
Seems I found a solution after a bit more tinkering around. 似乎我在稍微修补一下之后找到了解决方案。
The problem was: <ColumnDefinition Width="Auto"/>
问题是: <ColumnDefinition Width="Auto"/>
This was causing the column to fit to the content. 这导致列适合内容。 I changed it to: <ColumnDefinition />
我将其更改为: <ColumnDefinition />
This causes the column to fit to the empty space left in the parent container, regardless of content size. 无论内容大小如何,这都会使列适合父容器中剩余的空白区域。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.