【问题标题】:Make a grid to be closer to the top of the page - XAML使网格更靠近页面顶部 - XAML
【发布时间】:2015-03-23 07:07:57
【问题描述】:

我有以下 XAML:

<Grid    HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Column="1" >
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition Width="0*"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>

                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <PasswordBox HorizontalAlignment="Center" Grid.Row="0" Margin="12" Width="260" Height="32" PlaceholderText="Confirm Password"  BorderBrush="#FF755CB0" BorderThickness="1" Opacity="0.9" x:Name="ConfirmPassword"/>
            <Button Content="Sign-Up" HorizontalAlignment="Center" Margin="12" Grid.Row="1"  Width="260" Height="50" Background="#FF235085" BorderBrush="#FF6749AC" BorderThickness="1" Foreground="White" Opacity="0.9" RequestedTheme="Light" Click="Register_Click"/>
        </Grid>

现在网格位于屏幕的中心,我希望它稍微高一点,我可以用margin 做到这一点,但我不想在我的页面中使用pixel 的任何属性。还有其他方法吗? 谢谢。

【问题讨论】:

    标签: xaml position margin


    【解决方案1】:

    在不使用边距的情况下让网格略高于中心的最简单方法是将其放在另一个网格中。

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition Height="1.2*"/>
        </Grid.RowDefinitions>
        <Grid HorizontalAlignment="Center" VerticalAlignment="Top" Grid.Row="1" >
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition Width="0*"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <PasswordBox HorizontalAlignment="Center" Grid.Row="0" Margin="12" Width="260" Height="32" PlaceholderText="Confirm Password"  BorderBrush="#FF755CB0" BorderThickness="1" Opacity="0.9" x:Name="ConfirmPassword"/>
            <Button Content="Sign-Up" HorizontalAlignment="Center" Margin="12" Grid.Row="1"  Width="260" Height="50" Background="#FF235085" BorderBrush="#FF6749AC" BorderThickness="1" Foreground="White" Opacity="0.9" RequestedTheme="Light" Click="Register_Click"/>
        </Grid>
    </Grid>
    

    注意第二个 RowDefinition

    <RowDefinition Height="1.2*"/>
    

    这意味着第二行将比第一行占用更多的可用空间,您可以根据需要进行调整。

    【讨论】:

    • 谢谢。但是现在连clode都没有居中,内容在页面底部...
    • 不应该。启动一个测试应用程序,只需输入上面的代码,看看它是如何工作的,然后根据你的应用程序进行调整,因为它可能与代码中的其他网格、面板等有关
    • 如果我想让它靠近顶部,我必须将 更改为 我不知道为什么.在我的页面中,我只有那个网格,没有其他 XAML 元素
    • 是的,这意味着第二行比第一行大 8 倍。
    猜你喜欢
    • 2014-04-23
    • 1970-01-01
    • 2018-01-03
    • 2021-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-13
    相关资源
    最近更新 更多