UniformFill image centered

I have a little problem, I have a group item that I want to give the background image, which should scale while keeping the correct dimensions, but by default it displays the image from the top left corner, I want the image to be centered.

enter image description here

this is my code

 <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" />

      

+3


source to share


1 answer


You need to remove the width and height of the image - you want it to fill the container so that it uses the properties of the container. Also, if you want it to be centered, you need to define VerticalAlignment (and possibly horizontal). I think what you are looking for is:

 <Border BorderThickness="2" Width="180" Height="101" Grid.RowSpan="2" VerticalAlignment="Top">
     <Image Source="{Binding Imageurl}" HorizontalAlignment="Center" VerticalAlignment="Center" Stretch="UniformToFill"/>
 </Border>

      



You also don't need Grid.Column="0" Grid.RowSpan="2"

to Image

- these properties will have no effect - the image container is the Border (which is inside the Grid) and there you have to define the Column / RowSpan.

+4


source







All Articles