我有一个小问题,我有一个组项目,我想给它一个背景图像,它应该缩放以保持它的正确尺寸,但默认情况下它从左上角显示图像,我希望图像居中。
这是我的代码
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="15"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="180" />
<ColumnDefinition Width="300" />
</Grid.ColumnDefinitions>
<Border BorderThickness="2" Width="180" Height="101" Grid.RowSpan="2" VerticalAlignment="Top">
<Image Source="{Binding Imageurl} Width="180" Height="101" Grid.Column="0" Grid.RowSpan="2" HorizontalAlignment="Left" Stretch="UniformToFill"></Image>
</Border>
<TextBlock Text="Example1" Grid.Column="1" Grid.Row="0" VerticalAlignment="Top" />
<TextBlock Text="Example2" Grid.Column="1" Grid.Row="1" VerticalAlignment="Bottom" />
最佳答案
您需要删除Image 的Width 和Height - 您希望它填充容器,因此它应该使用容器的属性。此外,如果您希望它居中,则需要定义 VerticalAlignment(也可能是水平对齐)。我认为您正在寻找的是:
<Border BorderThickness="2" Width="180" Height="101" Grid.RowSpan="2" VerticalAlignment="Top">
<Image Source="{Binding Imageurl}" HorizontalAlignment="Center" VerticalAlignment="Center" Stretch="UniformToFill"/>
</Border>
您也不需要 Image
中的 Grid.Column="0"Grid.RowSpan="2"
- 这些属性不会生效 - 图像的容器是 Border(位于 Grid 内),您应该在其中定义 Column/RowSpan。
关于windows-phone-8 - 图像 UniformFill 居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26870637/