WPF和Silverlight集合类型接口汇总
原创文章,如需转载,请注明出处。
最近一直在整理WPF和Silverlight下数据绑定控件的问题,其中一个点就是关于数据源的。WPF和Silverlight下数据绑定控件的数据源往往就是一个集合,例如IEnumerable,ICollectionView等等。
.Net Framework定义了很多的集合类型,每一个都略有不同,都有自己的特性。好记性不如烂笔头,在这里总结一下,以备后用。
1. Collection Basic
IEnumerable and IEnumerable<T>
IEnumerable<T> is just like IEnumerable, but strongly typed.
public interface IEnumerable
{
IEnumerator GetEnumerator();
}
public interface IEnumerable<T> : IEnumerable
{
IEnumerator<T> GetEnumerator();
}
ICollection and ICollection<T>
ICollection<T> seems like ICollection, but it’s actually a very different abstraction. We found that ICollection was not very useful. At the same time, we did not have an abstraction that represented an read/write non-indexed collection. ICollection<T> is such abstraction and you could say that ICollection does not have an exact corresponding peer in the generic world.
public interface ICollection : IEnumerable
{
void CopyTo(Array array, int index);
int Count { get; }
bool IsSynchronized { get; }
object SyncRoot { get; }
}
public interface ICollection<T> : IEnumerable<T>, IEnumerable
{
void Add(T item);
void Clear();
bool Contains(T item);
void CopyTo(T[] array, int arrayIndex);
bool Remove(T item);
int Count { get; }
bool IsReadOnly { get; }
}
IList and IList<T>
IList<T> is just strongly typed IList, and just remove the IsFixedSize property.
public interface IList : ICollection, IEnumerable
{
int Add(object value);
void Clear();
bool Contains(object value);
int IndexOf(object value);
void Insert(int index, object value);
void Remove(object value);
void RemoveAt(int index);
bool IsFixedSize { get; }
bool IsReadOnly { get; }
object this[int index] { get; set; }
}
public interface IList<T> : ICollection<T>, IEnumerable<T>, IEnumerable
{
int IndexOf(T item);
void Insert(int index, T item);
void RemoveAt(int index);
T this[int index] { get; set; }
}
List<T> vs. Collection<T>
Difference:Collection<T> is made for extensibility and List<T> is made for performance,and the List<T> is optimized for speed, size, and power. Use List<T> for all your heavy lifting internally, and expose a Collection<T> in public API.
public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable
{
//...
}
public class Collection<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable
{
//...
}
这些是.Net Framework 1.0 或者2.0定义的最基本的集合类型接口,以上是一个简单的总结。
2. Collection in WPF and Silverlight
下面的这些接口是WPF、Silverlight下做数据绑定需要重点关注的。
INotifyCollectionChanged(.Net 3.0)
Notifies listeners of dynamic changes, such as when items get added and removed or the whole list is refreshed.
public interface INotifyCollectionChanged
{
event NotifyCollectionChangedEventHandler CollectionChanged;
}
INotifyPropertyChanged(.Net 2.0):
Notifies clients that a property value has changed, typically binding clients, that a property value has changed.
public interface INotifyPropertyChanged
{
event PropertyChangedEventHandler PropertyChanged;
}
ICollectionView(.Net 3.0)
Enables collections to have the functionalities of current record management, custom sorting, filtering, and grouping.
A collection view is a layer on top of a binding source collection that allows user to navigate and display the source collection based on sort, filter, and group queries, without having to change the underlying source collection itself. A collection view also maintains a pointer to the current item in the collection. If the source collection implements the INotifyCollectionChanged interface, the changes raised by the CollectionChanged event are propagated to the views.
public interface ICollectionView : IEnumerable, INotifyCollectionChanged
{
event EventHandler CurrentChanged;
event CurrentChangingEventHandler CurrentChanging;
bool Contains(object item);
IDisposable DeferRefresh();
bool MoveCurrentTo(object item);
bool MoveCurrentToFirst();
bool MoveCurrentToLast();
bool MoveCurrentToNext();
bool MoveCurrentToPosition(int position);
bool MoveCurrentToPrevious();
void Refresh();
bool CanFilter { get; }
bool CanGroup { get; }
bool CanSort { get; }
CultureInfo Culture { get; set; }
object CurrentItem { get; }
int CurrentPosition { get; }
Predicate<object> Filter { get; set; }
ObservableCollection<GroupDescription> GroupDescriptions { get; }
ReadOnlyObservableCollection<object> Groups { get; }
bool IsCurrentAfterLast { get; }
bool IsCurrentBeforeFirst { get; }
bool IsEmpty { get; }
SortDescriptionCollection SortDescriptions { get; }
IEnumerable SourceCollection { get; }
}
ICollectionViewFactory(.Net 3.0)
An interface that enables implementing collections to create a view to their data. Normally, user code does not call methods on this interface.
public interface ICollectionViewFactory
{
ICollectionView CreateView();
}
IPagedCollectionView(Silverlight 3.0)
Defines methods and properties that a collection view implements to provide paging capabilities to a collection.
public interface IPagedCollectionView
{
event EventHandler<EventArgs> PageChanged;
event EventHandler<PageChangingEventArgs> PageChanging;
bool MoveToFirstPage();
bool MoveToLastPage();
bool MoveToNextPage();
bool MoveToPage(int pageIndex);
bool MoveToPreviousPage();
bool CanChangePage { get; }
bool IsPageChanging { get; }
int ItemCount { get; }
int PageIndex { get; }
int PageSize { get; set; }
int TotalItemCount { get; }
}
IEditableCollectionView(.Net 3.5)
Defines methods and properties that a CollectionView implements to provide editing capabilities to a collection.
public interface IEditableCollectionView
{
object AddNew();
void CancelEdit();
void CancelNew();
void CommitEdit();
void CommitNew();
void EditItem(object item);
void Remove(object item);
void RemoveAt(int index);
bool CanAddNew { get; }
bool CanCancelEdit { get; }
bool CanRemove { get; }
object CurrentAddItem { get; }
object CurrentEditItem { get; }
bool IsAddingNew { get; }
bool IsEditingItem { get; }
NewItemPlaceholderPosition NewItemPlaceholderPosition { get; set; }
}
IEditableCollectionViewAddNewItem(WPF .Net 3.5)
Defines methods and properties that a CollectionView implements to enable specifying adding items of a specific type.
public interface IEditableCollectionViewAddNewItem : IEditableCollectionView
{
Object AddNewItem(Object newItem);
bool CanAddNewItem { get; }
}
关于IEditableCollectionView这个接口,参见我的文章:http://www.cnblogs.com/tedzhao/archive/2010/11/12/IEditableObject_and_IEditableCollectionView.html
看完了接口,再来几个集合类。
ObservableCollection(.Net 3.0)
Represents a dynamic data collection that provides notifications when items get added, removed, or when the whole list is refreshed.
public class ObservableCollection<T> : Collection<T>, INotifyCollectionChanged,
INotifyPropertyChanged
{
//...
}
PagedCollectionView(Silverlight 3.0)
Represents a view for grouping, sorting, filtering, and navigating a paged data collection.
public sealed class PagedCollectionView : ICollectionView, IEnumerable,
INotifyCollectionChanged, IPagedCollectionView,
IEditableCollectionView, INotifyPropertyChanged
{
//...
}
CollectionView(WPF, .Net 3.0)
Represents a view for grouping, sorting, filtering, and navigating a data collection.
public class CollectionView : DispatcherObject, ICollectionView,
IEnumerable, INotifyCollectionChanged, INotifyPropertyChanged
{
//...
}
这几个集合类型是WPF\Silverlight上最常见的数据源。由于Silverlight是Web应用,所以单独定义了一个IPagedCollectionView接口用来支持分页。
3. IQueryable and IEnumerable(更新于2011.03.01)
IEnumerable<T> is great for working with in-memory collections, but IQueryable<T> allows for a remote data source, like a database or web service.
IEnumerable doesn’t have the concept of moving between items, it is a forward only collection. something that most any data source can provide. Using only this minimal functionality, LINQ can provide all of these great operators.
IQueryable<T> is a very powerful feature that enables a variety of interesting deferred execution scenarios (like paging and composition based queries).