我遇到了动态添加字段属性的问题。
如果我这样做
<Rectangle Name="field_0_0" Width="10" Height="10" />
然后在C#代码中我可以通过
来实现field_0_0.Width = 20;
但在我的应用程序中,我做过类似的事情
for(i = 0; i < 5; i++) {
for(j = 0; j < 5; j++) {
String fieldName = "field_" + i + "_" + j;
Rectangle rec = new Rectangle();
rec.Name = fieldName;
Container.Children.Add(rec);
}
}
现在的问题是,当我在屏幕上显示这些字段时,我不知道如何调用这些字段?例如,我想更改field_1_1
如何从名称中获取此矩形?
答案 0 :(得分:2)
您可以使用FindName方法。
object findRect = Container.FindName("field_1_1");
if (findRect is Rectangle)
{
Rectangle rect = findRect as Rectangle;
//change rect properties
}