通用应用程序中心图像垂直和水平(xaml / wpf)

时间:2014-07-28 10:11:06

标签: wpf xaml win-universal-app

我尝试使用网格(在网格内)

<Grid HorizontalAlignment="Stretch" Background="#2f1807" Grid.Column="1" Grid.Row="0">
    <Grid.RowDefinitions>
        <RowDefinition Height="71"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition Width="505"/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <Image Source="Assets/logo.png" Height="71" Width="505" Grid.Column="1" Grid.Row="0" Stretch="None" />
</Grid>

图像的高度为71像素,宽度为505像素。这是输出: enter image description here 所以水平中心工作,但没有垂直,图像宽度应该填满整个单元格。为什么这不起作用?

uniform的输出:

enter image description here

图片道具(对不起荷兰语)尺寸为505宽,71像素高。

enter image description here

编辑@YoupTube建议:

<Image 
    Source="Assets/logo.png" 
    Height="71" 
    Width="505" 
    Grid.Column="1" 
    Grid.Row="0" 
    MinWidth="505" 
    MinHeight="71" 
    VerticalAlignment="Center" />

给出:

enter image description here

2 个答案:

答案 0 :(得分:5)

您的问题是,您已限制Height的{​​{1}},因此其大小与Grid相同。解决方案是删除Image上的Height设置(或删除整个RowDefinition)并让RowDefinition填充其父容器的空间... 然后 Grid可以垂直居中:

Image

更新&gt;&gt;&gt;

为了使<Grid HorizontalAlignment="Stretch" Background="#2f1807" Grid.Column="1" Grid.Row="0"> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition Width="505"/> <ColumnDefinition/> </Grid.ColumnDefinitions> <Image Source="Assets/logo.png" Height="71" Width="505" Grid.Column="1" Grid.Row="0" Stretch="None" HorizontalAlignment="Center" VerticalAlignment="Center" /> </Grid> 垂直居中,必须位于Image值大于Height的容器中。我看到您的Image位于另一个Grid ...如果Grid占据了此Image的全部Height,那么您必须将此Grid放入Grid 1}}进入一个容器,其HeightGrid<Grid HorizontalAlignment="Stretch" VerticalAlignment="Center" Background="#2f1807" Grid.Column="1" Grid.Row="0" MinHeight="150"> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition Width="505"/> <ColumnDefinition/> </Grid.ColumnDefinitions> <Image Source="Assets/logo.png" Height="71" Width="505" Grid.Column="1" Grid.Row="0" Stretch="None" HorizontalAlignment="Center" VerticalAlignment="Center" /> </Grid> 并代之以:

{{1}}

答案 1 :(得分:0)

你不应该只需要设置:

<Image VerticalAlignment="Center" ... />