WPF网格列全宽

时间:2020-03-13 14:23:52

标签: wpf grid width

我在使网格列变为全宽时遇到困难。如您所见,我在文本块上尝试了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>

结果:

screenshot

谢谢。

1 个答案:

答案 0 :(得分:1)

我建议使用1个网格,而不是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>
相关问题