我有这个代码:
public void LoadData1(String link)
{
string temp="";
int pos = 0;
for (int x = 0, y = 0; x < link.Length; x++) //separate various code
if (link[x] == 'y')
{
for (temp = ""; y < x; y++)
if(link[y]!='y')
temp += link[y];
list[pos] = temp;
pos++;
y = x++;
}
string nota=list[0];
for (int a = 0; a < list.Count() && list[a]!=null ; a++,nota=list[a])
{
string tempI = "";
//immagine
for (int x = 0; x < nota.Count(); x++)
{
if (nota[x] == 'p')
tempI += '+';
else
tempI += nota[x];
}
//nome della pagina
string temp1 = "";
int c = tempI.Count();
if (tempI.Count() > 2)
if (tempI.ElementAt(2) == 'l') //sol
{
for (int x = 0; x < c; x++)
if (x == 3 && tempI.ElementAt(3) == 'd')
temp1 += '#';
else
temp1 += tempI.ElementAt(x);
}
else
{
for (int x = 0; x < c; x++)
if (x == 2 && tempI.ElementAt(2) == 'd')
temp1 += '#';
else
temp1 += tempI.ElementAt(x);
}
else
temp1 = tempI;
this.Temp.Add(new ImageText() { Text = temp1, linkImage = "/Accordi/"+tempI+".png" });
}
this.IsDataLoaded1 = true;
this.Temp = new ObservableCollection<ImageText>();
}
这填充了一个由文本和图像组成的列表框。但我有这个问题:当我用 3 个元素填充列表框时(例如),一切正常,但是当我再次调用用 2 个元素填充列表框的函数时,列表框显示调用的两个元素,并在第三个元素空间显示我之前通话的元素!我该如何解决这个问题? 列表框通过 temp 进行绑定(bind),字符串链接包含一系列代码,如“DoyDodyRey”
最佳答案
每次加载数据时,您都会设置“第 n 个”元素,但是您对已经存在的内容没有做任何事情,因此,如果您在上一次调用中有第三个元素,而在后续调用中只有两个元素,则第三个仍然会在那里。
这只是先清除它,或随后删除多余的项目的问题。很难从代码中确切地看出发生了什么,但我认为在方法顶部调用 list.Clear()
可能会完成这项工作。
如果你想简单地删除多余的项目,我认为你应该能够在第一个 for 之后添加 while (list.Count() >= pos) list.Remove(pos);
循环。
关于c# - 我如何清空列表框内容c#?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13474559/