简体   繁体   中英

Programmatical grid Row definition not working

I have a window with that structure: 在此处输入图片说明

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. 在此处输入图片说明

在此处输入图片说明

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

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM