WPF使用自动调整大小的列适合行到网格宽度

时间:2017-03-16 09:22:26

标签: wpf grid width autosize

我将网格行拟合到父网格宽度时遇到问题。所以代码是(表头):

<Grid Height="Auto" Margin="20" VerticalAlignment="Top" >
    <Grid.RowDefinitions>
        <RowDefinition />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>

    <Label Grid.Column="0" Grid.Row="0" >Word</Label>
    <Label Grid.Column="1" Grid.Row="0" >Class</Label>
    <Label Grid.Column="2" Grid.Row="0" >Match case</Label>
    <Label Grid.Column="3" Grid.Row="0" >Regular expression</Label>
    <Label Grid.Column="4" Grid.Row="0" >Commands</Label>
</Grid>

问题是:如果基于Width="Auto"生成的行的所有列都不适合父网格。我知道我可以通过在其中一列上设置Width="*"来解决这个问题,但这不是我想要的。

我需要在所有列上自动调整大小以适应网格宽度(可以比计算行的自动大小大得多)。我该如何存档?

2 个答案:

答案 0 :(得分:1)

如果您不想设置Width =“Auto”,请使用Viewbox,它正确适合网格。

 <Viewbox Stretch="Uniform"  VerticalAlignment="Top">
        <Grid Height="Auto" Margin="20">
            <Grid.RowDefinitions>
                <RowDefinition />
            </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>

            <Label Grid.Column="0" Grid.Row="0" >Word</Label>
            <Label Grid.Column="1" Grid.Row="0" >Class</Label>
            <Label Grid.Column="2" Grid.Row="0" >Match case</Label>
            <Label Grid.Column="3" Grid.Row="0" >Regular expression</Label>
            <Label Grid.Column="4" Grid.Row="0" >Commands</Label>
        </Grid>
 </Viewbox>

答案 1 :(得分:0)

您可以使用SharedSizeGroup属性在属于不同ColumnDefinition的几个不同Grid之间共享大小调整属性:https://msdn.microsoft.com/en-us/library/system.windows.controls.definitionbase.sharedsizegroup(v=vs.110).aspx

<Grid Height="Auto" Margin="20" VerticalAlignment="Top" >
    <Grid.RowDefinitions>
        <RowDefinition />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition SharedSizeGroup="a" />
        <ColumnDefinition SharedSizeGroup="b" />
        <ColumnDefinition SharedSizeGroup="c" />
        <ColumnDefinition SharedSizeGroup="d" />
        <ColumnDefinition SharedSizeGroup="e" />
    </Grid.ColumnDefinitions>

    <Label Grid.Column="0" Grid.Row="0" >Word</Label>
    <Label Grid.Column="1" Grid.Row="0" >Class</Label>
    <Label Grid.Column="2" Grid.Row="0" >Match case</Label>
    <Label Grid.Column="3" Grid.Row="0" >Regular expression</Label>
    <Label Grid.Column="4" Grid.Row="0" >Commands</Label>
</Grid>

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition SharedSizeGroup="a" />
        <ColumnDefinition SharedSizeGroup="b" />
        <ColumnDefinition SharedSizeGroup="c" />
        <ColumnDefinition SharedSizeGroup="d" />
        <ColumnDefinition SharedSizeGroup="e" />
    </Grid.ColumnDefinitions>
    <TextBlock Grid.Column="0" Grid.Row="0" TextWrapping="Wrap">Some long word.....</TextBlock>
    <!-- -->
</Grid>