我的网格
:
// 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);
}