【发布时间】:2012-03-21 11:17:44
【问题描述】:
我创建了一个文本框控件数组,它们位于网格中,但我无法访问该框来更改文本。到目前为止,我试图通过以下方式解决它:
fields[row, col].IsEnabled = true;
fields[row, col].IsReadOnly = false;
但是没有用。
private void Window_Loaded(object sender, RoutedEventArgs e)
{
int wdth = 40;
int hght = 32;
for (int row = 0; row <= 8; row++)
{
ColumnDefinition colDef = new ColumnDefinition();
RowDefinition rowDef = new RowDefinition();
colDef.MinWidth = wdth;
colDef.MaxWidth = wdth;
rowDef.MinHeight = hght;
rowDef.MaxHeight = hght;
grdAdtn.ColumnDefinitions.Add(colDef);
grdAdtn.RowDefinitions.Add(rowDef);
};
TextBox[,] fields = new TextBox[9, 9];
FontFamily font = new FontFamily("Courier New");
for(int row=0; row <= 8; row++){
for(int col = 0; col <= 8; col++){
fields[row,col]=new TextBox();
fields[row, col].Name = "txt" + row.ToString() + "i"+col.ToString();
if ((row==0)&&(col==0))
{
fields[row, col].Text = "+";
}
else if (row == 0)
{
fields[row, col].Text = (col+1).ToString();
}
else if (col == 0)
{
fields[row, col].Text = (row+1).ToString();
}
else
{
fields[row, col].Text = "??";
}
fields[row, col].FontSize = 22;
fields[row, col].FontFamily = font;
fields[row, col].MaxLength = 2;
grdAdtn.Children.Add((fields[row, col]));
Grid.SetColumn(fields[row,col], col);
Grid.SetRow(fields[row,col], row);
fields[row, col].Width = wdth-2;
fields[row, col].Height = hght-2;
fields[row, col].VerticalAlignment = VerticalAlignment.Center;
fields[row, col].HorizontalAlignment = HorizontalAlignment.Center;
fields[row, col].HorizontalContentAlignment = HorizontalAlignment.Center;
fields[row, col].VerticalContentAlignment = VerticalAlignment.Center;
fields[row, col].IsEnabled = true;
fields[row, col].IsReadOnly = false;
}
}
}
有什么线索吗?
我也可以用fields[row,col].Focus() 聚焦它们,因此它们显然已启用,但我无法编写文本,当我用鼠标单击某些文本框时,它不会将焦点转移到该文本框上(按 tab 有效,但我需要更多功能)。据我所知,它根本没有做任何事情。
动态创建 TextBox 后是否需要手动添加 MouseDown/MouseUp 的事件处理程序?
【问题讨论】: