将OberservableCollection分配给ListBox并排除一些条目

时间:2013-02-04 19:04:50

标签: c# listbox observablecollection inheritance

我有从一个接口继承的不同类,所有这些类都被添加到OberservableCollection中。有人可以解释我如何将Observablecollection的所有值分配给我的列表框,但b类除外。

假设(编辑):

private void start()
{
    DP = new MyClass();
    this.ListBox.ItemsSource = new ObservableCollection<WInstance>(MyClass.Handler.Instances.OfType<WInstance>());
}

...
class Handler() : INotifyPropertyChanged
{
   public ObservableCollection<HandlerItem> Instances { get; private set; } 
 ...
}

class HandlerItem: INotifyPropertyChanged
{
    public Instance inst { get; private set; } //Instance could be WInstance or CInstance
}

在运行时,MyClass从远程计算机接收WInstance或CInstance的一些对象。如果我尝试使用Instances.OfType()解决它,似乎我的ItemsSource wount被更新

1 个答案:

答案 0 :(得分:0)

var list = new List<Instance>();

//.. add all items to your list here.


ObservableCollection<Instance> OCI = new ObservableCollection<Instance>(list.OfType<classa>());
// the above will generate a new ObservableCollection which contains all items of type classa from list.