已经用一些数据填充了ObservableCollection,如果数据集不足以填充我的图形,我想将其填充。 为此,我将在循环内的索引零处插入数据,以便填充始终位于集合的开头。 第一次迭代效果很好,但下一次迭代失败,并显示
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
我想念什么?
private void PadDataSet<T>(ObservableCollection<T> dataSet, DateTime minDataTimePeriod, int minuteIntervals)
{
var paddedList = new List<DataItem>();
var neededIntervals = _timescaleHours * 60 / minuteIntervals + 1;
var actualIntervals = dataSet.Count();
var padCount = neededIntervals - actualIntervals;
for (var i = 0; i < padCount; i++)
{
var g = Activator.CreateInstance<T>();
var item = g as DataItem;
if (item != null)
{
item.TimePeriod = minDataTimePeriod.AddMinutes(-minuteIntervals * (i + 1));
dataSet.Insert(0, g);
}
}
}