I currently have a class in UWP that is a wrapper for a bunch of different types of lists, including a custom-list that I have built. This wrapper needs to bind to some sort of list, either ListView, ListBox, GridView, etc.
The problem is when I am trying to implement INotifyCollectionChanged
, it seems that the UI element doesn't attatch a handler to the CollectionChanged
or the PropertyChanged
handlers (handlers are always null
). However, changing the list from my custom list to an ObservableCollection
seems to work fine. What am I missing to get the UI to bind its collection changed to my class?
My current implementation looks like
public class MyWrapperList<T> : IList<T>, INotifyPropertyChanged, INotifyCollectionChanged
{
private IEnumerable<T> _source;
// Implement all interfaces here, including my custom GetEnumerator() and all my add, insert, remove, etc classes
}
Note that I would not like to inheret from ObservableCollection
like many other answers suggest, due to the fact that I'd like this is a wrapper looking at the original list.
EDIT: You can find a sample of the issue reproduceable on GitHub: https://github.com/nolanblew/SampleCollectionChanged/
In order to have the ListView
auto-bind to your collection, you must implement both INotifyCollectionChange
and IList
(note: that's the non-generic IList).
If you modify your sample code so that your custom list class implements IList
:
public class MyWrapperList<T> : IList<T>, INotifyPropertyChanged, INotifyCollectionChanged, IList
{
//... all your existing code plus: (add your own implementation)
#region IList
void ICollection.CopyTo(Array array, int index) => throw new NotImplementedException();
bool IList.IsFixedSize => throw new NotImplementedException();
bool IList.Contains(object value) => throw new NotImplementedException();
int IList.IndexOf(object value) => throw new NotImplementedException();
void IList.Insert(int index, object value) => throw new NotImplementedException();
void IList.Remove(object value) => throw new NotImplementedException();
int IList.Add(object value) => throw new NotImplementedException();
public bool IsSynchronized => throw new NotImplementedException();
public object SyncRoot { get; } = new object();
object IList.this[int index] {
get => this[index];
set => this[index] = (T) value;
}
#endregion
}
Then the CollectionChanged
is set when the button click event is fired.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.