我如何清空列表框内容c#?

时间:2012-11-20 13:43:33

标签: c# silverlight windows-phone-7

我有这段代码:

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绑定的,字符串链接包含一系列代码,如“DoyDodyRey”

2 个答案:

答案 0 :(得分:3)

每次加载数据时都会设置'nth'元素,但是你对已经存在的内容一无所知,所以如果你在之前的调用中有第三个元素,而在后续调用中只有两个元素,第三个仍然会在那里。

这只是首先清除它,或者之后删除多余的物品。从你的代码中很难看出究竟发生了什么,但我认为在方法顶部调用list.Clear()可能会完成这项工作。

如果你想删除多余的项目,我认为你应该能够在第一次for循环后添加while (list.Count() >= pos) list.Remove(pos);

答案 1 :(得分:0)

你不能在开头声明清单,所以它是空的

List<string> yourlist = new List<string>();

你不需要在名单中使用pos,你可以使用

yourlist.add(temp)

列表中的第一篇文章获取您的列表[0],依此类推

希望这有点帮助:)