WPF Grid is not stretching vertically

30,811

Try setting row height to *

<Grid Name="gridMain" >    
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
</Grid>

Similarly define ColumnDefinition and set Width to *

Also make sure grid is the one not stretching not the user control. You can easily do that by assigning some background color to Grid.

Share:
30,811
Donotalo
Author by

Donotalo

Yet to be discovered.

Updated on July 09, 2022

Comments

  • Donotalo
    Donotalo almost 2 years

    I've the following Grid in the main window:

    <Grid Name="gridMain" Width="Auto" VerticalAlignment="Stretch" Height="Auto">
    </Grid>
    

    I'm adding a UserControl dynamically in the grid. The main window only has this grid. I want the grid to be stretched both horizontally and vertically and should fill up entire window. The grid is stretching horizontally but doesn't stretch vertically. Any idea why? What do I need to do to make the grid stretch vertically?

    EDIT

    Here is the complete XAML.

    <Window x:Class="Sensitech.TurboValidator.UserControls.ConveyorBelt"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="ConveyorBelt" Height="329" Width="714" xmlns:my="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit">
        <Grid Name="gridMain" Width="Auto" VerticalAlignment="Stretch" Height="Auto" Background="Cyan">
    
        </Grid>
    </Window>
    
  • Donotalo
    Donotalo over 12 years
    thanks for the background color idea. i found that the grid is actually stretching. the user control isn't. what property will help stretch the user control?
  • Donotalo
    Donotalo over 12 years
    i fixed it. it was the height of the usercontrol. thanks again for back color idea.
  • Haris Hasan
    Haris Hasan over 12 years
    Well if main control in User Control is Grid then you can do as I have suggested. Similarly if you have stack panel try to set vertical allignment to stretch. It depends on controls you have in user control