【发布时间】: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