程序网格行定义不起作用

时间:2016-01-25 08:57:52

标签: c# wpf grid rowdefinition columndefinition

我有一个具有该结构的窗口: enter image description here

然后在构造函数窗口的代码后面添加以下代码:

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

通过这样做,我希望窗口根据父窗口的大小调整行和列的大小。但是发生的事情是,虽然水平拉伸起作用但垂直方向不起作用。 enter image description here

enter image description here

简而言之,我喜欢的是根据父窗口大小拉伸列/行的网格

1 个答案:

答案 0 :(得分:1)

尝试使用dockpanel而不是堆栈面板

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