【发布时间】:2014-04-19 13:30:20
【问题描述】:
我知道没有Append,但我相信这是正确的称呼方式(对吧?)。
现在我有一个ObservableCollection,我用它来向我的DataGrid 添加新项目。它工作得很好,但是如果我一次添加 10 个项目,第二次添加 15 个项目,我最终会得到 15 个项目,而不是 25 个。基本上,该集合会覆盖旧集合的每个集合。
我可以循环遍历旧集合并在包含新集合后再次添加它,但这似乎是非常愚蠢的事情。我确信必须有另一种方法来做到这一点。
这是我的类里面的集合的样子 (I have used code from here):
public class ProxiesList
{
public string proxy { get; set; }
public string proxySource { get; set; }
public bool proxyUsed { get; set; }
public bool toUse { get; set; }
public string working { get; set; }
ObservableCollection<ProxiesList> GridItems = new ObservableCollection<ProxiesList>();
public ObservableCollection<ProxiesList> _GridItems
{
get
{
return GridItems;
}
}
public void addTheData(MainWindow mw, List<string> proxyList)
{
foreach (string line in proxyList)
{
_GridItems.Add(new ProxiesList //add items to the collection
{
proxy = line.Split(';')[0],
proxySource = line.Split(';')[1],
proxyUsed = false,
toUse = true,
working = "n/a"
});
}
mw.dataGrid1.ItemsSource = GridItems; //bind the collection to the dataGrid1
GridItems = new ObservableCollection<ProxiesList>(); //create new instance of the observable collection
}
}
【问题讨论】: