【问题标题】:Is it possible to search for index of multi type ObservableCollection?是否可以搜索多类型 ObservableCollection 的索引?
【发布时间】:2017-05-09 02:34:26
【问题描述】:

我计划获取集合中多个项目的指定索引并填充 GUI。这可以通过 ObservableCollection 方法完成,还是需要探索其他方法?还使用 MVVM Light 工具包。

我从 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 循环,我能够返回所有项目,例如所描绘的输出。但是,我不需要全部打印。我需要给定索引处的项目。

【问题讨论】:

  • 所以您只需要某个特定索引上的 Item1 到 Item4?
  • 问题不清楚,你想让'index'做什么?你能用一些有意义的变量名来代替吗?

标签: c# wpf linq mvvm mvvm-light


【解决方案1】:

我不确定你的问题是什么。你的 Where 没有意义;你如何索引一个整数?

无论如何,collection[specifiedIndex].Item1 会从该索引处的集合中的对象中获取Item1 值。如果这就是你要问的。

如果你想要Item1 == 9的所有集合项,

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

我的意思是,它只是一个集合。就像一个列表,但它也碰巧引发了通知。

【讨论】:

  • 我只阅读了“在哪里”并且不知道如何使用它,但这绝对有效!谢谢埃德!
猜你喜欢
  • 1970-01-01
  • 2013-10-07
  • 2017-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-18
  • 2023-03-21
  • 1970-01-01
相关资源
最近更新 更多