自动调整网格列以占用父级中的剩余空间 [英] Autosizing a grid column to take up remaining space in parent

查看:102
本文介绍了自动调整网格列以占用父级中的剩余空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WPF中,我有一段时间试图让网格的大小适当。



我的网格布局如下:

 < ItemsControl Horizo​​ntalContentAlignment =Stretch> 
< ItemsControl.ItemTemplate>
< DataTemplate>
<网格>
< 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>

问题是宽度=自动似乎是根据内容的宽度,而不是填写父容器中的额外空间。这使得剩下的列全部未对齐,并在每行末尾留下难看的空白。



我可能错过了一些简单的东西,但我看起来不像找到一种方法适当地适合列。



或者是否有更好的控制工作?

解决方案

* 表示填写或分享。如果你有两个 * ,那么他们会平均分配宽度。

 < ColumnDefinition Width =*/> 


In WPF, I am having a heck of a time trying to get a grid to size properly.

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. 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="*"/>

这篇关于自动调整网格列以占用父级中的剩余空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆