Alex F
Alex F

Reputation: 43311

Image is not stretched to the whole client area

Visual Studio 2012, WPF C# Windows Application. XAML:

<Window x:Class="Edge.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <Grid Background="Blue">
        <Image Source="Images/House.png"  
           HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
           Stretch="Fill" Margin="3"
           />
    </Grid>
</Window>

Image is not stretched to the whole window, as I want, it is shown only in 1/4 window area. I expect that Stretch="Fill" must stretch the image to the whole window. What is wrong?

enter image description here

Upvotes: 0

Views: 882

Answers (1)

Dutts
Dutts

Reputation: 6191

The problem is your outer Grid. A Grid will re-size to fit the size of its children, but the child in this case (the Image) is trying to stretch to fill the size of its parent so I'm going to guess that what you are seeing is the image being drawn actual size.

Try using a DockPanel instead which fills all available parent space children.

<Window x:Class="Edge.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <DockPanel Background="Blue">
        <Image Source="Images/House.png"  
           HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
           Stretch="Fill" Margin="3"/>
    </DockPanel> 
</Window>

Upvotes: 2

Related Questions