2017-05-09 77 views
0

我打算獲取集合中多個項目的指定索引並填充GUI。這可以通過ObservableCollection方法完成,還是需要探索其他方法?也使用MVVM Light工具包。是否可以搜索多類型ObservableCollection的索引?

我開始用6個屬性:

ObservableCollection<Model> collection = new ObservableCollection(); 

Public Class Model : INotifyPropertyChanged { 

    private string _Item1; 
    public string Item1 
    { 
     get{ return _Item1;} 
     set{ Item1 = value; RaisedPropertyChange(nameof(Item1)); 
    } 

    private int _Item2; 
    public int Item2 
    { 
     get{ return _Item2;} 
     set{ Item1 = value; RaisedPropertyChange(nameof(Item2)); 
    } 
    . 
    . 
    . 
    . 
} 

作爲新MVVM,我不知道從哪裏開始就如何指定索引處得到的特定項目。我會使用LINQ方法嗎?

Where(i => i.Item1[SpecifiedIndex?]); 

例子:

Output: Collection Index 1:Item1, Item2, Item3, Item4. 
Output: Collection Index 2:Item1, Item2, Item3, Item4. 
Output: Collection Index 3:Item1, Item2, Item3, Item4. 

我卡在如何讓項目從收集的秩序。我已經添加了它們,如果我使用了一個Foreach循環,我可以返回所有項目,例如描述的輸出。但是,我不需要全部打印。我需要給定索引處的項目。

+0

所以你只需要Item1到Item4的某些特定索引? – sachin

+0

問題不清楚,你想要'索引'做什麼?你能替換一些有意義的變量名嗎? –

回答

1

我不確定你的問題是什麼。你的地方沒有道理;你如何索引一個整數?

無論如何,collection[specifiedIndex].Item1爲您獲取該索引集合中對象的Item1值。如果這就是你要求的。

如果你想去的地方Item1 == 9

var x = collection.Where(item => item.Item1 == 9); 

我的意思是所有的收藏品,它只是一個集合。像列表一樣,但它也恰好引發通知。

+0

我只讀過「哪裏」,不知道如何使用它,但這絕對有效!謝謝埃德! – ScottieA11

相關問題