这没什么特别的,来自 Silverlight/XAML 背景,但可能或多或少会起作用。您可能需要针对细微的 API 差异(在没有 VS 的记事本中编写)和未经测试的细微差异进行调整,但应该为您提供入门所需的内容。
private void fillDate(int offset)
{
int rows = 2;
int columns = 5;
int currentEntry = 1;
for(int rowIndex = 0; rowIndex < rows; rowIndex++)
{
for (int columnIndex = 0; columnIndex < columns; columnIndex++)
{
if (currentEntry > offset)
{
TextBlock textEntry = new TextBlock();
textEntry.Text = currentEntry.ToString();
Grid.SetRow(textEntry, rowIndex);
Grid.SetColumn(textEntry, columnIndex);
}
currentEntry++;
}
}
}
编辑:刚刚意识到您可能想要一个在“空”单元格中没有文本的 TextBlock,在这种情况下,将内部循环的代码替换为:
TextBlock textEntry = new TextBlock();
Grid.SetRow(textEntry, rowIndex);
Grid.SetColumn(textEntry, columnIndex);
if (currentEntry > offset)
textEntry.Text = currentEntry.ToString();
currentEntry++;
编辑:根据您的评论,在创建控件时首先运行一个方法来构建网格并填充所有文本字段并将它们存储在某种列表中:
private int Rows = 2;
private int Columns = 5;
private TextBlock[][] TextEntries;
private void CreateTextBlocks()
{
TextEntries = new TextBlock[Rows][];
for (int rowIndex = 0; rowIndex < rows; rowIndex++)
{
entries[rowIndex] = new string[columns];
for (int columnIndex = 0; columnIndex < columns; columnIndex++)
{
TextBlock textEntry = new TextBlock();
Grid.SetRow(textEntry, rowIndex);
Grid.SetColumn(textEntry, columnIndex);
myGrid.Children.Add(textEntry);
TextEntries[rowIndex][columnIndex] = textEntry;
}
}
}
随后运行另一种方法来根据需要更改值:
private void fillDate(int offset)
{
int currentEntry = 1;
for(int rowIndex = 0; rowIndex < Rows; rowIndex++)
{
for (int columnIndex = 0; columnIndex < Columns; columnIndex++)
{
TextBlock textEntry = TextEntries[rowIndex][columnIndex]
if (currentEntry > offset)
textEntry.Text = currentEntry.ToString();
else
textEntry.Text = String.Empty;
currentEntry++;
}
}
}