【问题标题】:Centering StackPanel in a Grid UWP在网格 UWP 中居中 StackPanel
【发布时间】:2019-08-04 00:46:26
【问题描述】:

对于我的通用 Windows 应用程序,我试图将 StackPanel 置于页面上的网格中,如图所示。但是当我运行下面的代码时,三个按钮被放置在底部的中心。可能是什么问题?

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>

编辑: 解决办法是:

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

删除网格中的VerticalAlignment="Top"

【问题讨论】:

  • 如果您已经解决了您的问题,请在下方发布您的答案,请避免在您的问题中发布答案。
  • 其实你只需要在最顶层的 Grid 中切换行定义。第一个使用 Height=Auto,第二个 *

标签: xaml win-universal-app uwp-xaml


【解决方案1】:

解决方案是设置合理的行高比。并从位于第 0 行区域的 Grid 中删除 VerticalAlignment="Top"

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

更新

根据您的要求,您无需在顶部区域设置自定义导航栏。你可以使用NavigationView 来接近。最新导航提供
顶部显示模式。

<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>

更多信息请参考NavigationViewofficial document

【讨论】:

  • 我不认为将高度设置为 0.1*(相对值)是一个好主意。它应该是绝对值或自动,因为标题的高度不依赖于父(窗口)高度
  • @Liero,是的,但是请给出他的解决方案,我只是帮助移到下面。对于他的要求,更好的方法是在顶部区域制作一个 NavigationView 并将 Frame 放在下面。
猜你喜欢
  • 2022-07-29
  • 1970-01-01
  • 2018-02-15
  • 1970-01-01
  • 2023-03-13
相关资源
最近更新 更多