代码之家  ›  专栏  ›  技术社区  ›  Jeronymite

C#WPF XAML自动增加网格行数等

  •  0
  • Jeronymite  · 技术社区  · 6 年前

    在XAML中,我在一个网格中定义了多个相同的行,每行有五个元素,如下所示:

        <Button    x:Name="Button1"     Grid.Row="8" Grid.Column="0"/>
        <TextBlock x:Name="TextBlockA1" Grid.Row="8" Grid.Column="1"/>
        <TextBlock x:Name="TextBlockB1" Grid.Row="8" Grid.Column="2"/>
        <TextBlock x:Name="TextBlockC1" Grid.Row="8" Grid.Column="3"/>
        <TextBlock x:Name="TextBlockD1" Grid.Row="8" Grid.Column="4"/>
    

    我可能有多达32排。两个问题:

    1. 如何创建包含五个元素的单个元素并将其添加到单个元素中,但仍然能够访问代码隐藏中包含的五个元素中的每一个?

    0 回复  |  直到 6 年前
        1
  •  0
  •   Anders H    6 年前

    我的网格 :

      // Class to hold the content on each row
      private class RowContent
      {
         public Button button { get; set; }
         public TextBlock textBlockA { get; set; }
         public TextBlock textBlockB { get; set; }
         public TextBlock textBlockC { get; set; }
         public TextBlock textBlockD { get; set; }
      }
      // List of row content on the grid
      private List<RowContent> gridRows;
      /// <summary>
      /// Build the content on the grid
      /// </summary>
      /// <param name="rowCount">Number of rows to build</param>
      private void buildGrid(int rowCount)
      {
         // The Grid to build
         Grid gridToBuild = this.MyGrid;
         // The rows on the grid
         gridRows = new List<RowContent>();
         // Make the column definitions
         gridToBuild.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });
         for (int colNum = 1; colNum < 5; colNum++)
         {
            gridToBuild.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) });
         }
         // Loop to make each row
         for (int rowNum=0; rowNum < rowCount; rowNum++)
         {
            // Make the row definition in the grid
            gridToBuild.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
            // Make the content on the row
            RowContent rowContent = new RowContent()
            {
               button = new Button() { Name = "Button" + rowNum.ToString(), Content="Button "+rowNum.ToString() },
               textBlockA = new TextBlock() { Name = "TextBlockA" + rowNum.ToString(), Text = "Text A " + rowNum.ToString() },
               textBlockB = new TextBlock() { Name = "TextBlockB" + rowNum.ToString(), Text = "Text B " + rowNum.ToString() },
               textBlockC = new TextBlock() { Name = "TextBlockC" + rowNum.ToString(), Text = "Text C " + rowNum.ToString() },
               textBlockD = new TextBlock() { Name = "TextBlockD" + rowNum.ToString(), Text = "Text D " + rowNum.ToString() }
            };
            // Add the controls on the row to the grid
            gridToBuild.Children.Add(rowContent.button);
            gridToBuild.Children.Add(rowContent.textBlockA);
            gridToBuild.Children.Add(rowContent.textBlockB);
            gridToBuild.Children.Add(rowContent.textBlockC);
            gridToBuild.Children.Add(rowContent.textBlockD);
            // Set the row number on each control
            Grid.SetRow(rowContent.button, rowNum);
            Grid.SetRow(rowContent.textBlockA, rowNum);
            Grid.SetRow(rowContent.textBlockB, rowNum);
            Grid.SetRow(rowContent.textBlockC, rowNum);
            Grid.SetRow(rowContent.textBlockD, rowNum);
            // Set the column number on each control
            Grid.SetColumn(rowContent.button, 0);
            Grid.SetColumn(rowContent.textBlockA, 1);
            Grid.SetColumn(rowContent.textBlockB, 2);
            Grid.SetColumn(rowContent.textBlockC, 3);
            Grid.SetColumn(rowContent.textBlockD, 4);
            // Add the row content to the list
            gridRows.Add(rowContent);
         }
      }
      /// <summary>
      /// Applying the template
      /// </summary>
      public override void OnApplyTemplate()
      {
         // Call base
         base.OnApplyTemplate();
         // Build the grid
         buildGrid(32);
      }