本文介绍了Extended ObservableCollection - 如何创建ToObservableCollectionEx()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 你好所有 我有这个 ObservableCollectionEx 类。 基本上,我想要的就是将 INotifyPropertyChanged 事件扩展到集合中任何成员的任何更改。 public partial class ObservableCollectionEx< T> :ObservableCollection< T> 其中 T:INotifyPropertyChanged { public ObservableCollectionEx(): base (){} public ObservableCollectionEx(List< T> list) : base ((list!= null )? new 列表< T>(list.Count):list) { CopyFrom(list); } public ObservableCollectionEx(IEnumerable< T> collection) { if (collection == null ) throw new ArgumentNullException( collection); CopyFrom(collection); } private void CopyFrom(IEnumerable< T> collection) ) { IList< T> items =项目; if (collection!= null && items!= null ) { using (IEnumerator< T> enumerator = collection.GetEnumerator()) { while (enumerator.MoveNext()) { items.Add(enumerator.Current); } } } } 受保护 override void InsertItem( int index,T item) { base .InsertItem(index,item); item.PropertyChanged + = Item_PropertyChanged; } 受保护 覆盖 void RemoveItem( int index) { Items [index] .PropertyChanged - = Item_PropertyChanged; base .RemoveItem(index); } 受保护 虚拟 void MoveItem( int oldIndex, int newIndex) { T removedItem = this [oldIndex]; base .RemoveItem(oldIndex); base .InsertItem(newIndex,removedItem); } 受保护 覆盖 void ClearItems() { foreach (T item in 项目) { item.PropertyChanged - = Item_PropertyChanged; } base .ClearItems(); } 受保护 覆盖 void SetItem( int index,T item) { T oldItem = Items [index]; T newItem = item; oldItem.PropertyChanged - = Item_PropertyChanged; newItem.PropertyChanged + = Item_PropertyChanged; base .SetItem(index,item); } private void Item_PropertyChanged( object sender,PropertyChangedEventArgs e) { var handler = ItemPropertyChanged; if (handler!= null ){handler(sender,e); } } public event PropertyChangedEventHandler ItemPropertyChanged; } 此外我有 ObservableCollection class,一个简单的 ToObservableCollection()方法。 public static class CollectionExtensions { public static ObservableCollection< T> ToObservableCollection< T>(此 IEnumerable< T> enumerableList) { return enumerableList!= null ? new ObservableCollection< T>(enumerableList): null ; } } 因为我需要ToObservableCollectionEx(),我构建它是一样的我对ToObservableCollection做的方式。 public static ObservableCollectionEx< T> ToObservableCollectionEx< T>(此 IEnumerable< T> enumerableList) { return enumerableList!= null ? new ObservableCollectionEx< T>(enumerableList): null ; } 但我收到此错误: 类型'T'不能在泛型类型或方法'Portal_Tools.ObservableCollectionEx< t>'中用作类型参数'T'。没有从'T'到'System.ComponentModel.INotifyPropertyChanged'的装箱转换或类型参数转换。 用Google搜索此消息,找不到可以解决此错误的任何答案。 尝试了不同的方法,但总是得到相同的错误,因为< T>总是在那里。 有谁可以帮我解决一下如何解决这个错误? 谢谢解决方案 我已经弄明白了!它正在工作。 public static ObservableCollectionEx< ; T> ToObservableCollectionEx< t>(此 IEnumerable< t> enumerableList)其中 T:INotifyPropertyChanged { return enumerableList!= null ? new ObservableCollectionEx< t>(enumerableList): null ; } < / t > < / t > ; < / t > < / t > 区别在于,其中T:INotifyPropertyChanged part。 Hallo AllI have this ObservableCollectionEx class.Basically, all I want from it is to extend the INotifyPropertyChanged event to any changes on any member of the collection.public partial class ObservableCollectionEx<T> : ObservableCollection<T> where T : INotifyPropertyChanged{ public ObservableCollectionEx() : base() { } public ObservableCollectionEx(List<T> list) : base((list != null) ? new List<T>(list.Count) : list) { CopyFrom(list); } public ObservableCollectionEx(IEnumerable<T> collection) { if (collection == null) throw new ArgumentNullException("collection"); CopyFrom(collection); } private void CopyFrom(IEnumerable<T> collection) { IList<T> items = Items; if (collection != null && items != null) { using (IEnumerator<T> enumerator = collection.GetEnumerator()) { while (enumerator.MoveNext()) { items.Add(enumerator.Current); } } } } protected override void InsertItem(int index, T item) { base.InsertItem(index, item); item.PropertyChanged += Item_PropertyChanged; } protected override void RemoveItem(int index) { Items[index].PropertyChanged -= Item_PropertyChanged; base.RemoveItem(index); } protected virtual void MoveItem(int oldIndex, int newIndex) { T removedItem = this[oldIndex]; base.RemoveItem(oldIndex); base.InsertItem(newIndex, removedItem); } protected override void ClearItems() { foreach (T item in Items) { item.PropertyChanged -= Item_PropertyChanged; } base.ClearItems(); } protected override void SetItem(int index, T item) { T oldItem = Items[index]; T newItem = item; oldItem.PropertyChanged -= Item_PropertyChanged; newItem.PropertyChanged += Item_PropertyChanged; base.SetItem(index, item); } private void Item_PropertyChanged(object sender, PropertyChangedEventArgs e) { var handler = ItemPropertyChanged; if (handler != null) { handler(sender, e); } } public event PropertyChangedEventHandler ItemPropertyChanged;}Additionally I have an extension to the ObservableCollection class, a simple ToObservableCollection() method.public static class CollectionExtensions{ public static ObservableCollection<T> ToObservableCollection<T>(this IEnumerable<T> enumerableList) { return enumerableList != null ? new ObservableCollection<T>(enumerableList) : null; }}As I needed the ToObservableCollectionEx(), I build it the same way I did to the ToObservableCollection.public static ObservableCollectionEx<T> ToObservableCollectionEx<T>(this IEnumerable<T> enumerableList){ return enumerableList != null ? new ObservableCollectionEx<T>(enumerableList) : null;}But I get this error:"The type 'T' cannot be used as type parameter 'T' in the generic type or method 'Portal_Tools.ObservableCollectionEx<t>'. There is no boxing conversion or type parameter conversion from 'T' to 'System.ComponentModel.INotifyPropertyChanged'."Googled around this message and couldn't find any answer that would solve this error.Tried different approaches but always got the same error because the <T> is always there.Can anyone help me out and give me a clue on how to solve this error?Thanks 解决方案 I have figured it out! It's working. public static ObservableCollectionEx<t> ToObservableCollectionEx<t>(this IEnumerable<t> enumerableList) where T : INotifyPropertyChanged { return enumerableList != null ? new ObservableCollectionEx<t>(enumerableList) : null; }</t></t></t></t>The difference is on the where T : INotifyPropertyChanged part. 这篇关于Extended ObservableCollection - 如何创建ToObservableCollectionEx()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-28 07:20