WPF网格列全宽

问题描述 投票:0回答:1

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

谢谢。

wpf grid width
1个回答
0
投票

我建议使用1个网格,而不是网格+ DockPanel +网格

<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>
© www.soinside.com 2019 - 2024. All rights reserved.