简体   繁体   English

循环和数组-显示字符串

[英]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] ,将String2ClassStringClass[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.

相关问题 遍历字符串数组并与以前的值进行比较 - Loop through an array of strings and compare to previous values 遍历数组,显示TextBox并等待ENTER - Loop through array, show TextBox and wait for ENTER 如何在不同文本框中的阵列中显示所有不同的字符串? - How do I show all different strings in an ARRAY in DIFFERENT textboxes? 使用foreach循环将多个字符串存储在Array中并以另一种形式显示 - Store multiple strings in Array using foreach loop and display in another form 如何在循环中逐个字符串显示 label 上的字符串数组? - How to display array of strings on a label in string by string in a loop? 有没有一种方法可以遍历数组,用C#编辑数组的内容,然后再次遍历它以显示编辑的内容? - Is there a way to loop through an array, edit the contents of an array in C#, and the loop through it again to show the edited contents? 连接每个循环中的字符串 - Concatenate Strings In For Each Loop 如何获得嵌套在另一个数据网格中以显示字符串子数组的数据网格? - How do I get a datagrid nested inside another datagrid to show a sub array of strings? 使用字符串和字符串数组完成if语句 - Complete if statement with strings and array of strings 通过这些字符串的一部分对字符串数组进行排序 - sort array of strings by a part of these strings
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM