[英]Loop and Array - Show Strings
I am trying to show a group variables that contains a string from the code behind (C#) to Textblock in XAML using the code below: 我正在尝试使用以下代码显示包含从(C#)后面的代码到XAML中Textblock的字符串的组变量:
Textblock1.Text = String1Class;
Textblock2.Text = String2Class;
Textblock3.Text = String3Class;
Textblock4.Text = String4Class;
Textblock5.Text = String5Class;
Textblock6.Text = String6Class;
The code works but its a pain writing the same thing over and over again. 该代码有效,但是一遍又一遍地写同样的事情很痛苦。 I want to ask if there is a better way.
我想问一下是否有更好的方法。 I know it has to do with arrays and loops, but I am not very familiar with C# and WPF.
我知道它与数组和循环有关,但是我对C#和WPF不太熟悉。
Given the fact that you have separate variables for each of the StringXClass
values you need to do this if you want to keep the variables the same: 鉴于每个
StringXClass
值都有单独的变量,因此如果要保持变量不变,则需要执行以下操作:
var tbs = new [] { Textblock1, Textblock2, Textblock3, Textblock4, Textblock5, Textblock6, };
var scs = new [] { String1Class, String2Class, String3Class, String4Class, String5Class, String6Class, };
for (var i = 0; i < tbs.Length; i++)
{
tbs.Text = scs;
}
The alternative is to set up an array in the first place. 另一种方法是首先设置一个数组。
var StringClass = new string[6];
Then replace String1Class
with StringClass[0]
, String2Class
with StringClass[1]
, etc, in your code. 然后在代码
String1Class
StringClass[0]
替换为StringClass[0]
,将String2Class
为StringClass[1]
等。
Then write this: 然后写这个:
var tbs = new [] { Textblock1, Textblock2, Textblock3, Textblock4, Textblock5, Textblock6, };
for (var i = 0; i < tbs.Length; i++)
{
tbs.Text = StringClass[i];
}
It very much depends on how you want to display the info. 这在很大程度上取决于您要如何显示信息。 You could use a
List
, for example, and write out the items to a TextBlock
. 例如,您可以使用
List
并将项目写出到TextBlock
。
List<string> stringsList = new List<string>() {
"string1",
"string2"
};
foreach (string s in stringsList)
{
textBlock1.Text += s + @"\n";
}
You can loop through the TextBlocks if they are in the same parent: 如果它们位于同一父级中,则可以遍历TextBlocks:
string controlName = "TextBlock";
int startIndex = 1;
int endIndex = 100;
List<string> stringList = new List<string>();
for(int i = 0; i < 100 ; i++)
stringList.Add("string"+(i+1).ToString());
for(int i = startIndex; i<=endIndex; i++)
{
foreach(control c in TextBlock1.Parent.Controls)//Or if you know the actual parent to which all the textBoxes belong
{
if(c.Name == (controlName+i))
{
(c as TextBlock).Text = stringsList[i-1]; //since our start index starts with 1.
break;
}
}
}
XAML: XAML:
<StackPanel>
<TextBlock Name="TextBlock1"></TextBlock>
<TextBlock Name="TextBlock2"></TextBlock>
<TextBlock Name="TextBlock3"></TextBlock>
<TextBlock Name="TextBlock4"></TextBlock>
</StackPanel>
Code: 码:
TextBlock[] textboxes = { TextBlock1, TextBlock2, TextBlock3, TextBlock4 };
string[] list = { "TextBlock 1", "TextBlock 2", "TextBlock 3", "TextBlock 4" };
for (int i = 0; i < textboxes.Length; i++)
{
textboxes[i].Text = list[i];
}
Output: 输出:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.