简体   繁体   中英

Auto-increment Grid Row Numbers Etc

In XAML, I define multiple identical rows in a Grid, each row with five elements, as follows:

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

I may have as many as 32 rows. Two questions:

  1. How may I create a single element that encompasses the five elements and add it in a single element, but still be able to access each of the five contained elements in code-behind?

  2. How may I auto-increment the index of the row (in this example, starting at 8) and the index within the string Names (in this example, starting at 1) so that I do not have to hard-code them all?

You can do this by using code like this in your window class, there you have a grid with the name MyGrid :

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

Later in your code you can access each row through the list gridRows

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