简体   繁体   中英

Centering StackPanel in a Grid UWP

For my Universal Windows App, I am trying to center a StackPanel in a Grid on Page like in the Picture. But when I run the code below, three buttons are placed centered at the bottom. What could be the problem?

在此处输入图片说明

MainPage.xaml:

<Page>
    <Grid Background="WhiteSmoke">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Grid VerticalAlignment="Top">
            <StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="10 0">
                <Button x:Name="BackButton" Height="50" Width="150" Background="{x:Null}" BorderBrush="White">
                    <StackPanel Orientation="Horizontal">
                        <Viewbox MaxHeight="50" MaxWidth="50">
                            <SymbolIcon Symbol="Back" Foreground="White"></SymbolIcon>
                        </Viewbox>
                        <TextBlock Foreground="White" VerticalAlignment="Center" Margin="10" FontSize="20" FontWeight="Bold">Back</TextBlock>
                    </StackPanel>
                </Button>
            </StackPanel>
            <StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center">
                <Button Width="150" Height="50" Content="Page 1" FontSize="20" FontWeight="Bold"/>
                <Button Width="150" Height="50" Content="Page 2" Foreground="#FFFFFF" FontSize="20" FontWeight="Bold"/>
                <Button Width="150" Height="50" Content="Page 3" Foreground="#FFFFFF" FontSize="20" FontWeight="Bold"/>
            </StackPanel>
            <Grid Width="150" Height="5" Background="#FFFFFF" HorizontalAlignment="Center" Margin="240" />
        </Grid>
        <Frame
            Grid.Row="1"
            x:Name="Frame">
        </Frame>
    </Grid>
</Page>

Page1.xaml

<Page>
    <Grid Background="WhiteSmoke">
        <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center">
            <Button Height="150" Width="300" Margin="5" FontWeight="Bold" FontSize="20">
                <StackPanel Orientation="Vertical">
                    <TextBlock Foreground="White" VerticalAlignment="Center" Margin="10" Padding="0">Page 1</TextBlock>
                </StackPanel>
            </Button>
            <Button Height="150" Width="300" Margin="5" FontWeight="Bold" FontSize="20">
                <StackPanel Orientation="Vertical">
                    <TextBlock Foreground="White" VerticalAlignment="Center" Margin="10" Padding="0">Page 2</TextBlock>
                </StackPanel>
            </Button>
            <Button Height="150" Width="300" Margin="5" FontWeight="Bold" FontSize="20">
                <StackPanel Orientation="Vertical">
                    <TextBlock Foreground="White" VerticalAlignment="Center" Margin="10" Padding="0">Page 3</TextBlock>
                </StackPanel>
            </Button>
        </StackPanel>
    </Grid>
</Page>

Edit : the solution is:

<Grid.RowDefinitions>
    <RowDefinition Height="0.1*"/>
    <RowDefinition Height="*"/>
</Grid.RowDefinitions>

Delete VerticalAlignment="Top" in Grid

The solution is setting a reasonable row height ratio. And remove VerticalAlignment="Top" from Grid where placed in Row 0 area.

<Grid.RowDefinitions>
    <RowDefinition Height="0.1*"/>
    <RowDefinition Height="*"/>
</Grid.RowDefinitions>

Update

For your requirement, you have no need set a custom navigation bar in the top area. You could use NavigationView to approach. The latest Navigation provide
Top display mode. 在此处输入图片说明

<NavigationView x:Name="nvSample" Header="This is Header Text" PaneDisplayMode="Top">
    <NavigationView.MenuItems>
        <NavigationViewItem  Content="Menu Item1" Tag="SamplePage1" />
        <NavigationViewItem  Content="Menu Item2" Tag="SamplePage2" />
        <NavigationViewItem  Content="Menu Item3" Tag="SamplePage3" />
        <NavigationViewItem  Content="Menu Item4" Tag="SamplePage4" />
    </NavigationView.MenuItems>
    <Frame x:Name="contentFrame"/>
</NavigationView>

For more please refer NavigationView official document .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM