问题
I am building an object model with strongly typed collection classes (e.g. CustomerCollection
). I want to support full two-way binding on both the collection itself and all of the data models in the collection.
For the models it seems like implementing INotifyPropertyChanged
is the right way to wire up the models. But what inferface/base class should I use so that WPF knows when my collection's contents change?
回答1:
ObservableCollection<T> - designed specifically for WPF binding.
回答2:
I would recommend typing your properties that you expose as IList, or IEnumerable (generic or not, your choice), rather than ObservableCollection since it ties you into that implementation, and there are a number of situations that this becomes annoying.
The specific interface you need your collections to implement is INotifyCollectionChanged.
来源:https://stackoverflow.com/questions/2124072/wpf-what-is-the-right-base-class-for-collections-used-in-data-binding