Programmatical grid Row definition not working

769 views Asked by At

I have a window with that structure: enter image description here

Then in the code behind of the window in the constructor I add the following code:

int numRow = 2;
  int numColumn = 3;

  for (int row = 0; row < numRow; row++)
  {
    var rd = new RowDefinition();
    rd.Height = new GridLength(1.0, GridUnitType.Star);
    grdMain.RowDefinitions.Add(rd);
  }

  for (int column = 0; column < numColumn; column++)
  {
    var cd = new ColumnDefinition();
    cd.Width = new GridLength(1.0, GridUnitType.Star);
    grdMain.ColumnDefinitions.Add(cd);
  }

  for (int row = 0; row < numRow; row++)
  {
    for (int column = 0; column < numColumn; column++)
    {
      //var borderImage = new Border();
      //borderImage.BorderThickness = new Thickness(1);
      //borderImage.BorderBrush = new SolidColorBrush(Colors.Red);

      var finalImage = new Image();
      BitmapImage logo = new BitmapImage();
      logo.BeginInit();
      logo.UriSource = new Uri("pack://application:,,,/EasyRun;component/Resources/Images/ITA.png");
      logo.EndInit();
      finalImage.Source = logo;

      //borderImage.Child = finalImage;

      Grid.SetRow(finalImage, row);
      Grid.SetColumn(finalImage, column);
      grdMain.Children.Add(finalImage);
    }

by doing so I expect the window to resize rows and columns accordin to the size of the parent window. But what happens is that while the horizontal stretch works the vertical doesn't. enter image description here

enter image description here

So in short what I'd like is the grid to stretch columns/rows according to its parent window size

1

There are 1 answers

0
Luca On BEST ANSWER

Try using a dockpanel instead then a stack panel

 <DockPanel Name="stpMain">
 <Button DockPanel.Dock="Bottom" Content="AAA" Width="100" Click="Button_Click"></Button>
 <Border BorderThickness="2" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="0" BorderBrush="Red">
   <Grid Name="grdMain"></Grid>
 </Border>