1 回答
TA贡献1862条经验 获得超7个赞
如果您的文本框都共享一个公共父容器(即网格),那么迭代它们就非常容易。XAML 可能看起来像这样:
<Grid Name="textBoxContainer">
<!-- row, column definitions omitted -->
<TextBox />
<TextBox />
<TextBox />
<!-- etc... -->
</Grid>
...然后在代码隐藏文件中,您需要像这样迭代这些文本框:
int showBoxCount = 4; // this number gets loaded from your file elsewhere
foreach (var textBox in textBoxContainer.Children.OfType<TextBox>()) {
if (showBoxCount > 0)
textBox.Visibility = Visibility.Visible;
else
textBox.Visibility = Visibility.Collapsed;
showBoxCount--;
}
如果文本框不共享公共父容器,那么您需要为每个文本框指定一个名称,然后在代码隐藏文件中手动将它们放入数组中。
XAML:
<TextBox Name="txt1" />
<TextBox Name="txt2" />
<TextBox Name="txt3" />
<!-- etc... -->
隐藏代码:
var textBoxes = new TextBox[] { txt1, txt2, txt3, etc... };
int showBoxCount = 4; // this number gets loaded from your file elsewhere
foreach (var textBox in textBoxes) {
if (showBoxCount > 0)
textBox.Visibility = Visibility.Visible;
else
textBox.Visibility = Visibility.Collapsed;
showBoxCount--;
}
- 1 回答
- 0 关注
- 78 浏览
添加回答
举报