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

本文关键字:排除 OberservableCollection 分配 ListBox | 更新日期: 2023-09-27 17:57:27

我从一个接口继承了不同的类,所有这些类都添加到ObservableCollection中。有人能告诉我如何将Observablecollection的所有值分配给我的Listbox吗

假设(编辑):

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似乎不会被更新

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

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.