我已经创建了一个文本框的控件数组,它们位于网格中但我无法访问该框以更改文本。到目前为止,我试图解决它:
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()
将它们聚焦,因此它们显然已启用,但我无法编写文本,当我在某些TextBox上单击鼠标时,它不会将焦点转移到该TextBox上(按Tab键工作)但我需要更多功能)。就我所见,它根本没有做任何事情。
在动态创建TextBox后,是否需要为MouseDown / MouseUp手动添加事件处理程序?
答案 0 :(得分:0)
我将你的Window_Loaded粘贴到解决方案中,构建并且它工作得很好 - 我可以更改生成的文本框中的文本就好了...除了
之外你还有什么在XAML中<Grid x:Name="grdAdtn"/>