WPF 网格中心和拉伸文本框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15843214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:29:40  来源:igfitidea点击:

WPF Grid Center and Stretch Textbox

wpfgridcenterstretch

提问by Greg Hill

I have a WPF grid with row and column 0 as labels. I want the first column to be a static value of 20, but I want the rest of the columns to stretch to their available space until they hit the max width and I want the grid to remain centered even after the rows have hit their max. If I use Grid HorizontalAlignment="Center" each column width is equal to the width of the content of that row, which doesn't look very good for a column full of empty text boxes. If I use Grid HorizontalAlignment="Stretch" it does exactly what I want it to but the Grid isn't centered after the columns have reached their max width, it's left aligned. Is there any way to center the grid and stretch the columns? Attached is a section of the xaml for my grid.

我有一个 WPF 网格,其中第 0 行和第 0 列作为标签。我希望第一列的静态值是 20,但我希望其余的列伸展到它们的可用空间,直到它们达到最大宽度,并且我希望网格即使在行达到最大值后也能保持居中。如果我使用 Grid Horizo​​ntalAlignment="Center" 每列宽度等于该行内容的宽度,这对于充满空文本框的列来说看起来不太好。如果我使用 Grid Horizo​​ntalAlignment="Stretch" 它完全符合我的要求,但是在列达到最大宽度后,网格不会居中,而是左对齐。有没有办法使网格居中并拉伸列?附件是我的网格的一部分 xaml。

Thanks.

谢谢。

<Expander Margin="10,10,10,10" Header="Expander">
    <Grid Margin="10,10,10,10" HorizontalAlignment="Center">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="20" />
            <ColumnDefinition Width="*" MaxWidth="200"/>
            <ColumnDefinition Width="*" MaxWidth="200" />
            <ColumnDefinition Width="*" MaxWidth="200" />
         </Grid.ColumnDefinitions>
         <Grid.RowDefinitions>
             <RowDefinition />
             <RowDefinition />
         </Grid.RowDefinitions>
         <Label Content="Ax" HorizontalContentAlignment="Center" Grid.Column="1" Grid.Row="0"/>
         <Label Content="0" HorizontalContentAlignment="Center" VerticalAlignment="Center" Grid.Column="0" Grid.Row="1" />
         <TextBox Name="AxK00" Margin="1,1,1,1" HorizontalContentAlignment="Center" Grid.Column="1" Grid.Row="1" />
         <TextBox Name="AyK00" Margin="1,1,1,1" HorizontalContentAlignment="Center" Grid.Column="2" Grid.Row="1" />
         <TextBox Name="AzK00" Margin="1,1,1,1" HorizontalContentAlignment="Center" Grid.Column="3" Grid.Row="1" />
    </Grid>
</Expander>

回答by Dan Busha

While your columns have reached their MaxWidth the Grid has a HorizontalAlignment=Stretch which means the Grid is going to expand to fill the space unless you give it a Width. Try setting your Grid's MaxWidth to 620 (the sum of your MaxWidths) or something similar. Your Grid will stretch to fill your max size, but still be small enough for the HorizontalAlignment =Center to be observable.

当您的列达到它们的 MaxWidth 时,Grid 有一个 Horizo​​ntalAlignment=Stretch,这意味着除非您给它一个 Width,否则 Grid 将扩展以填充空间。尝试将 Grid 的 MaxWidth 设置为 620(MaxWidths 的总和)或类似的值。您的 Grid 将拉伸以填充您的最大尺寸,但仍然足够小,以便 Horizo​​ntalAlignment =Center 可以观察到。