2017-07-07 94 views
0

我有一个oxyplot色谱柱系列,我用线性轴作为直方图进行了掩盖。我从20个元素的频率列表中获得我的值。Oxyplot色谱柱系列 - 使用循环添加项目

我不知道是否有这样做一个聪明的方法:

this.Items = new Collection<Item> 
{ 
    new Item {Label = "1", Value=frequency[0]}, 
    new Item {Label = "2", Value=frequency[1]}, 
    new Item {Label = "3", Value=frequency[2]}, 
    ... 
    new Item {Label = "18", Value=frequency[17]}, 
    new Item {Label = "19", Value=frequency[18]}, 
    new Item {Label = "20", Value=frequency[19]}, 
}; 

我试图中创建一个for循环是这样的:

this.Items = new Collection<Item> 
{ 
    for (int i = 0; i < 20; i++) 
    { 
     Items.Add(new Item { Label = i.ToString(), Value = frequency[i]}); 
    } 
}; 

但它不工作。

有没有人有关于如何做到这一点的想法?

+2

把for循环* *以外的对象初始化器。 –

+0

创建空的Collection对象。在循环collectionObj.Add(...)中执行从1到20的循环;循环后this.Items = collectionObj ;. – mybirthname

+1

此外,对于未来 - “不起作用”不是一个足够的问题描述。请包括(在这种情况下)你得到的编译器错误,以及运行时错误,无论你得到什么异常。 –

回答

1

您不能将for循环放入对象初始值设定项中。

  1. 创建集合。

    this.Items = new Collection<Item>(); 
    
  2. 填充它:

    for (int i = 0; i< 20; i++) 
    { 
        Items.Add(new Item { Label = i.ToString(), Value = frequency[i] }); 
    }