垂直对齐不起作用XAML

时间:2015-01-23 17:08:41

标签: c# xaml grid alignment center

如何使用XAML / C#将Windows应用商店应用集中在屏幕中间,包括其所有内容?

我试过了:

<Grid HorizontalAlignment="Center" VerticalAlignment="Center">

但似乎vertical alignment无效并且网格停留在屏幕顶部。

编辑:我希望红色部分位于屏幕中央:

enter image description here

 <Grid RequestedTheme="Light" Loaded="Grid_Loaded" Background="{ThemeResource AppBarItemBackgroundThemeBrush}">
        <Grid.Resources>
            <Storyboard x:Name="FadeImageStoryboard">
                <DoubleAnimation From="0" 
                                 To="1" 
                                 Duration="0:0:0.7"
                                 Storyboard.TargetName="BackgroundImage"
                                 Storyboard.TargetProperty="Opacity" />
            </Storyboard>
        </Grid.Resources>
        <Grid.ChildrenTransitions>
            <TransitionCollection>
                <EntranceThemeTransition/>
            </TransitionCollection>
        </Grid.ChildrenTransitions>
        <Image Source="assets/113h.jpg" Stretch="Fill"  x:Name="BackgroundImage" RequestedTheme="Light" Margin="0,-10,0,10" />
        <Button  x:Name="backButton" Style="{StaticResource NavigationBackButtonNormalStyle}"
                        Margin="23,45,0,0" 
                        VerticalAlignment="Top"
                        Command="{Binding NavigationHelper.GoBackCommand, ElementName=pageRoot}"
                        AutomationProperties.Name="Back"
                        AutomationProperties.AutomationId="BackButton"
                        AutomationProperties.ItemType="Navigation Button" Click="backButton_Click"/>
        <StackPanel x:Name="Container">
            <Grid    HorizontalAlignment="Center" VerticalAlignment="Center"> 
                <TextBox Margin="363,172,368,308" PlaceholderText="Last Name" BorderBrush="#FF755CB0" BorderThickness="1" Opacity="0.9" x:Name="LastName"/>                  

                <Button Content="Sign-Up" HorizontalAlignment="Left" Margin="360,417,0,0" VerticalAlignment="Top" Width="257" Height="50" Background="#FF235085" BorderBrush="#FF6749AC" BorderThickness="1" Foreground="White" Opacity="0.9" RequestedTheme="Light" Click="Register_Click"/>
            </Grid>
        </StackPanel>
    </Grid>

1 个答案:

答案 0 :(得分:0)

您的Grid包含在StackPanel中。这意味着它在堆栈面板中居中。考虑到StackPanel的工作方式,这无论如何都没有多大意义。

取出Grid(将其设为根Grid的子项),并且中心将按预期工作。

 <Grid RequestedTheme="Light" Loaded="Grid_Loaded" Background="{ThemeResource AppBarItemBackgroundThemeBrush}">
   ...
   //No StackPanel!
    <Grid HorizontalAlignment="Center" VerticalAlignment="Center"> 
        <TextBox Margin="363,172,368,308" PlaceholderText="Last Name" BorderBrush="#FF755CB0" BorderThickness="1" Opacity="0.9" x:Name="LastName"/>      
        <Button Content="Sign-Up" HorizontalAlignment="Left" Margin="360,417,0,0" VerticalAlignment="Top" Width="257" Height="50" Background="#FF235085" BorderBrush="#FF6749AC" BorderThickness="1" Foreground="White" Opacity="0.9" RequestedTheme="Light" Click="Register_Click"/>
    </Grid>
    ....
</Grid>