【发布时间】:2018-11-29 19:42:46
【问题描述】:
所以我有五个文本块添加到一个列表中,每个文本块都应该有自己的 1 到 6 之间的随机数。
我知道我可以为每个文本块 (int randomNumberOne, randomNumberTwo, etc) 做一个新的 int,但我想看看我是否可以弄清楚如何为每个循环制作一个列表和一个。
有没有办法在列表中编辑文本框的内容?如果有,我还没有找到任何方法。
这是我目前的代码。
List<TextBlock> randomBoxList = new List<TextBlock>();
public MainWindow()
{
InitializeComponent();
randomBoxList.Add(randomBoxOne);
randomBoxList.Add(randomBoxTwo);
randomBoxList.Add(randomBoxThree);
randomBoxList.Add(randomBoxFour);
randomBoxList.Add(randomBoxFive);
}
Random randomGenerator = new Random();
int randomNumber;
private void randomButton_Click(object sender, RoutedEventArgs e)
{
foreach (TextBlock textBlock in randomBoxList)
{
randomNumber = randomGenerator.Next(1, 7);
//Code to change randomBox content goes here.
}
}
【问题讨论】:
-
wpf 还是 wnforms?
-
WPF。抱歉,我会编辑我的帖子以说明这一点。
-
我为此推荐 MVVM。然后你可以将它绑定到 ContentControl 并循环遍历一个列表。
-
我将要提出的建议。如果您将按钮定义为循环容器类型的元素(如列表等),这可能会有点工作
-
虽然使用 MVVM 绝对是一个不错的建议,但您也可以这样做
textBlock.Text = randomNumber.ToString()
标签: c# wpf list random textblock