编程网格行定义不起作用

Programmatical grid Row definition not working

我有一个具有该结构的 window:

然后在构造函数window后面的代码中添加如下代码:

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);
    }

通过这样做,我希望 window 能够根据父 window 的大小调整行和列的大小。但是发生的事情是,虽然水平拉伸有效,但垂直拉伸无效。

所以简而言之,我想要的是根据其父 window 大小

拉伸 columns/rows 的网格

尝试使用停靠面板而不是堆栈面板

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