【问题标题】:c# convert ObservableCollection to another typec# 将 ObservableCollection 转换为另一种类型
【发布时间】:2023-04-06 11:40:01
【问题描述】:

我有以下问题让我陷入困境。在下面的示例中,我创建了 A 类和 B 类,并从 B 类继承了 A 类。然后我创建了一个 ObservableCollection<A> 来保存一组人。然后我想做的是扩展 A 类以包含 B 类中的属性,并将其创建为我可以更新的引用副本 ObservableCollection。

我得到的错误是:

无法转换类型的对象 '<CastIterator>d__b1'1[iheriatance_obseravale_collection.Form1+B]' 到 类型 'System.Collections.ObjectModel.ObservableCollection'1[iheriatance_obseravale_collection.Form1+B]'.

public class A : INotifyPropertyChanged
{
    string _firstName;
    public string firstName
    {
        get
        {
            return _firstName;
        }
        set
        {
            if (_firstName != value)
            {
                _firstName = value;
                NotifyPropertyChanged("firstName");
            }
        }
    }

    string _surname;
    public string surname
    {
        get
        {
            return _surname;
        }
        set
        {
            if (_surname != value)
            {
                _surname = value;
                NotifyPropertyChanged("surname");
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public void NotifyPropertyChanged(string PropName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(PropName));
        }
    }
}

public class B : A, INotifyPropertyChanged
{
    bool _isSelected;
    public bool isSelected
    {
        get
        {
            return _isSelected;
        }
        set
        {
            if (_isSelected != value)
            {
                _isSelected = value;
                NotifyPropertyChanged("isSelected");
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public void NotifyPropertyChanged(string PropName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(PropName));
        }
    }
}

ObservableCollection<A> peopleColl = new ObservableCollection<A>();

private void addData()
{
    peopleColl.Add(new A() { firstName = "James", surname = "Smith" });
    peopleColl.Add(new A() { firstName = "John", surname = "Woods" });
}

ObservableCollection<B> selectedPeople;

private void selectAllPeople()
{
    selectedPeople = (ObservableCollection<B>)peopleColl.Cast<B>();

    foreach (B person in selectedPeople)
    {
        person.isSelected = true;
        Console.WriteLine("Surname:{0}, IsSelected:{1}", person.surname, person.isSelected);
    }
} 

非常感谢 斯图尔特·特伯菲尔德

【问题讨论】:

    标签: c# inheritance type-conversion observablecollection


    【解决方案1】:

    你必须迭代源,转换每个对象,然后创建一个新集合:

     selectedPeople = new ObservableCollection<B>(peopleColl.OfType<B>());
    

    仅当 peopleColl 包含 B 对象时才有效。如果它只包含A,则需要从A 创建B 的新实例:

     selectedPeople = new ObservableCollection<B>(peopleColl.Select(i => new B(i));
    
    public class B : A
    {
        public B()
        {
        }
    
        public B(A a)
        {
           this.firstName = a.firstName;
           this.surname = a.surname;
        }
    }
    

    仅供参考,如果A 实现INotifyPropertyChanged,并且如果B 继承自A,则无需在B 类中再次实现INotifyPropertyChanged

    【讨论】:

      猜你喜欢
      • 2020-12-02
      • 2018-06-25
      • 2020-12-14
      • 1970-01-01
      • 2012-03-28
      • 2012-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多