首页 > 解决方案 > WPF网格列全宽

问题描述

我很难让我的网格列全宽。如您所见,我在文本块上尝试了 Horizo​​ntalAlignment="Stretch"(也尝试了 Center)和 Width="*"(也尝试了 Auto),但似乎都不起作用。

我希望该列是整个窗口宽度,并且“欢迎”文本居中。

<Grid>
        <DockPanel LastChildFill="False">
            <TextBlock DockPanel.Dock="Top" Text="Drink &amp; Drive"/>
            <TextBlock DockPanel.Dock="Bottom" Text="Drink &amp; Drive - 2020"/>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <TextBlock Grid.Column="0" Grid.Row="0" HorizontalAlignment="Stretch" Text="WELCOME"/>
            </Grid>
        </DockPanel>
    </Grid>

结果:

截屏

谢谢。

标签: wpfgridwidth

解决方案


我建议使用 1 Grid,而不是 Grid + DockPanel + Grid

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>

    <TextBlock Grid.Row="0" Text="Drink &amp; Drive"/>        

    <TextBlock Grid.Row="1" Text="WELCOME" HorizontalAlignment="Center" VerticalAlignment="Center"/>

    <TextBlock Grid.Row="2" Text="Drink &amp; Drive - 2020"/>
</Grid>

推荐阅读