Skip to main content
.NET Framework Class Library
ItemCollection Class

Holds the list of items that constitute the content of an ItemsControl.

Namespace:   System.Windows.Controls
Assembly:  PresentationFramework (in PresentationFramework.dll)
Syntax
<[%$TOPIC/ms611035_en-us_VS_110_3_0_0_0_0%]([%$TOPIC/ms611035_en-us_VS_110_3_0_0_0_1%].Ignore)> _
Public NotInheritable Class ItemCollection _
	Inherits [%$TOPIC/ms611035_en-us_VS_110_3_0_0_0_2%] _
	Implements [%$TOPIC/ms611035_en-us_VS_110_3_0_0_0_3%], [%$TOPIC/ms611035_en-us_VS_110_3_0_0_0_4%], [%$TOPIC/ms611035_en-us_VS_110_3_0_0_0_5%], [%$TOPIC/ms611035_en-us_VS_110_3_0_0_0_6%],  _
	[%$TOPIC/ms611035_en-us_VS_110_3_0_0_0_7%], [%$TOPIC/ms611035_en-us_VS_110_3_0_0_0_8%], [%$TOPIC/ms611035_en-us_VS_110_3_0_0_0_9%], [%$TOPIC/ms611035_en-us_VS_110_3_0_0_0_10%]
[[%$TOPIC/ms611035_en-us_VS_110_3_0_1_0_0%]([%$TOPIC/ms611035_en-us_VS_110_3_0_1_0_1%].Ignore)]
public sealed class ItemCollection : [%$TOPIC/ms611035_en-us_VS_110_3_0_1_0_2%], 
	[%$TOPIC/ms611035_en-us_VS_110_3_0_1_0_3%], [%$TOPIC/ms611035_en-us_VS_110_3_0_1_0_4%], [%$TOPIC/ms611035_en-us_VS_110_3_0_1_0_5%], [%$TOPIC/ms611035_en-us_VS_110_3_0_1_0_6%], [%$TOPIC/ms611035_en-us_VS_110_3_0_1_0_7%], 
	[%$TOPIC/ms611035_en-us_VS_110_3_0_1_0_8%], [%$TOPIC/ms611035_en-us_VS_110_3_0_1_0_9%], [%$TOPIC/ms611035_en-us_VS_110_3_0_1_0_10%]
[[%$TOPIC/ms611035_en-us_VS_110_3_0_2_0_0%]([%$TOPIC/ms611035_en-us_VS_110_3_0_2_0_1%]::Ignore)]
public ref class ItemCollection sealed : public [%$TOPIC/ms611035_en-us_VS_110_3_0_2_0_2%], 
	[%$TOPIC/ms611035_en-us_VS_110_3_0_2_0_3%], [%$TOPIC/ms611035_en-us_VS_110_3_0_2_0_4%], [%$TOPIC/ms611035_en-us_VS_110_3_0_2_0_5%], [%$TOPIC/ms611035_en-us_VS_110_3_0_2_0_6%], [%$TOPIC/ms611035_en-us_VS_110_3_0_2_0_7%], 
	[%$TOPIC/ms611035_en-us_VS_110_3_0_2_0_8%], [%$TOPIC/ms611035_en-us_VS_110_3_0_2_0_9%], [%$TOPIC/ms611035_en-us_VS_110_3_0_2_0_10%]
[<[%$TOPIC/ms611035_en-us_VS_110_3_0_3_0_0%]>]
[<[%$TOPIC/ms611035_en-us_VS_110_3_0_3_0_1%]([%$TOPIC/ms611035_en-us_VS_110_3_0_3_0_2%].Ignore)>]
type ItemCollection =  
    class 
        inherit [%$TOPIC/ms611035_en-us_VS_110_3_0_3_0_3%] 
        interface [%$TOPIC/ms611035_en-us_VS_110_3_0_3_0_4%] 
        interface [%$TOPIC/ms611035_en-us_VS_110_3_0_3_0_5%] 
        interface [%$TOPIC/ms611035_en-us_VS_110_3_0_3_0_6%] 
        interface [%$TOPIC/ms611035_en-us_VS_110_3_0_3_0_7%] 
        interface [%$TOPIC/ms611035_en-us_VS_110_3_0_3_0_8%] 
        interface [%$TOPIC/ms611035_en-us_VS_110_3_0_3_0_9%] 
        interface [%$TOPIC/ms611035_en-us_VS_110_3_0_3_0_10%] 
        interface [%$TOPIC/ms611035_en-us_VS_110_3_0_3_0_11%] 
    end

The ItemCollection type exposes the following members.

Properties
  NameDescription
Public property CanChangeLiveFilteringGets a value that indicates whether the collection view supports turning filtering data in real time on or off.
Public property CanChangeLiveGroupingGets a value that indicates whether the collection view supports turning grouping data in real time on or off.
Public property CanChangeLiveSortingGets a value that indicates whether the collection view supports turning sorting data in real time on or off.
Public property CanFilterGets a value that indicates whether this collection view supports filtering. (Overrides CollectionViewCanFilter.)
Public property CanGroupGets a value that indicates whether this collection view supports grouping. (Overrides CollectionViewCanGroup.)
Public property CanSortGets a value that indicates whether this collection view supports sorting. (Overrides CollectionViewCanSort.)
Public property ComparerReturns an object that you can use to compare items in the view. (Inherited from CollectionView.)
Public property CountGets the number of records in the collection. (Overrides CollectionViewCount.)
Public property CultureGets or sets the culture information to use during sorting. (Inherited from CollectionView.)
Public property CurrentItemGets the current item in the view. (Overrides CollectionViewCurrentItem.)
Public property CurrentPositionGets the ordinal position of the current item within the view. (Overrides CollectionViewCurrentPosition.)
Public property DispatcherGets the Dispatcher this DispatcherObject is associated with. (Inherited from DispatcherObject.)
Public property FilterGets or sets a callback used to determine if an item is suitable for inclusion in the view. (Overrides CollectionViewFilter.)
Public property GroupDescriptionsGets a collection of GroupDescription objects that defines how to group the items. (Overrides CollectionViewGroupDescriptions.)
Public property GroupsGets the top-level groups that are constructed according to the GroupDescriptions. (Overrides CollectionViewGroups.)
Public property IsCurrentAfterLastGets a value that indicates whether the current item of the view is beyond the end of the collection. (Overrides CollectionViewIsCurrentAfterLast.)
Public property IsCurrentBeforeFirstGets a value that indicates whether the current item of the view is beyond the beginning of the collection. (Overrides CollectionViewIsCurrentBeforeFirst.)
Public property IsEmptyGets a value that indicates whether the resulting (filtered) view is empty. (Overrides CollectionViewIsEmpty.)
Public property IsInUseGets a value that indicates whether any object is subscribing to the events of this CollectionView. (Inherited from CollectionView.)
Public property IsLiveFilteringGets or sets a value that indicates whether filtering data in real time is enabled.
Public property IsLiveGroupingGets or sets a value that indicates whether grouping data in real time is enabled.
Public property IsLiveSortingGets or sets a value that indicates whether sorting in real time is enabled.
Public property ItemGets or sets the item at the given zero-based index.
Public property LiveFilteringPropertiesGets a collection of strings that specify the properties that participate in filtering data in real time.
Public property LiveGroupingPropertiesGets a collection of strings that specify the properties that participate in grouping data in real time.
Public property LiveSortingPropertiesGets a collection of strings that specify the properties that participate in sorting data in real time.
Public property NeedsRefreshGets a value that indicates whether the collection needs to be refreshed. (Overrides CollectionViewNeedsRefresh.)
Public property SortDescriptionsGets a collection of SortDescription objects that describe how the items in the collection are sorted in the view. (Overrides CollectionViewSortDescriptions.)
Public property SourceCollectionGets the unsorted and unfiltered collection that underlies this collection view. (Overrides CollectionViewSourceCollection.)
Top
Methods
  NameDescription
Public method AddAdds an item to the ItemCollection.
Public method CheckAccessDetermines whether the calling thread has access to this DispatcherObject. (Inherited from DispatcherObject.)
Public method ClearClears the collection and releases the references on all items currently in the collection.
Public method ContainsReturns a value that indicates whether the specified item is in this view. (Overrides CollectionViewContains(Object).)
Public method CopyToCopies the elements of the collection to an array, starting at a particular array index.
Public method DeferRefreshEnters a defer cycle that you can use to merge changes to the view and delay automatic refresh. (Overrides CollectionViewDeferRefresh.)
Public method DetachFromSourceCollectionRemoves the reference to the underlying collection from the CollectionView. (Inherited from CollectionView.)
Public method Equals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public method GetHashCodeServes as the default hash function. (Inherited from Object.)
Public method GetItemAtReturns the item at the specified zero-based index in this view. (Overrides CollectionViewGetItemAt(Int32).)
Public method GetTypeGets the Type of the current instance. (Inherited from Object.)
Public method IndexOfReturns the index in this collection where the specified item is located. (Overrides CollectionViewIndexOf(Object).)
Public method Insert Inserts an element into the collection at the specified index.
Public method MoveCurrentToSets the specified item in the collection as the CurrentItem. (Overrides CollectionViewMoveCurrentTo(Object).)
Public method MoveCurrentToFirstSets the first item in the view as the CurrentItem. (Overrides CollectionViewMoveCurrentToFirst.)
Public method MoveCurrentToLastSets the last item in the view as the CurrentItem. (Overrides CollectionViewMoveCurrentToLast.)
Public method MoveCurrentToNextSets the item after the CurrentItem in the view as the CurrentItem. (Overrides CollectionViewMoveCurrentToNext.)
Public method MoveCurrentToPositionSets the item at the specified index to be the CurrentItem in the view. (Overrides CollectionViewMoveCurrentToPosition(Int32).)
Public method MoveCurrentToPreviousSets the item before the CurrentItem in the view as the CurrentItem. (Overrides CollectionViewMoveCurrentToPrevious.)
Public method PassesFilterReturns a value that indicates whether the specified item belongs to this view. (Overrides CollectionViewPassesFilter(Object).)
Public method RefreshRe-creates the view. (Inherited from CollectionView.)
Public method RemoveRemoves the specified item reference from the collection or view.
Public method RemoveAtRemoves the item at the specified index of the collection or view.
Public method ToStringReturns a string that represents the current object. (Inherited from Object.)
Public method VerifyAccessEnforces that the calling thread has access to this DispatcherObject. (Inherited from DispatcherObject.)
Top
Events
  NameDescription
Public event CurrentChangedOccurs after the CurrentItem has changed. (Inherited from CollectionView.)
Public event CurrentChangingOccurs when the CurrentItem is changing. (Inherited from CollectionView.)
Top
Extension Methods
  NameDescription
Public Extension Method AsParallelOverloaded. Enables parallelization of a query. (Defined by ParallelEnumerable.)
Public Extension Method AsParallelOverloaded. (Defined by ParallelEnumerable.)
Public Extension Method AsQueryableOverloaded. Converts an IEnumerable to an IQueryable. (Defined by Queryable.)
Public Extension Method AsQueryableOverloaded. (Defined by Queryable.)
Public Extension Method CastTResultOverloaded. Casts the elements of an IEnumerable to the specified type. (Defined by Enumerable.)
Public Extension Method CastTResultOverloaded. (Defined by Enumerable.)
Public Extension Method OfTypeTResultOverloaded. Filters the elements of an IEnumerable based on a specified type. (Defined by Enumerable.)
Public Extension Method OfTypeTResultOverloaded. (Defined by Enumerable.)
Top
Explicit Interface Implementations
  NameDescription
Explicit interface implemetation Private property ICollectionIsSynchronizedThis member supports the Windows Presentation Foundation (WPF) infrastructure and is not intended to be used directly from your code.
Explicit interface implemetation Private property ICollectionSyncRootThis member supports the Windows Presentation Foundation (WPF) infrastructure and is not intended to be used directly from your code.
Explicit interface implemetation Private method IEditableCollectionViewAddNewAdds a new item to the collection.
Explicit interface implemetation Private property IEditableCollectionViewCanAddNewGets a value that indicates whether a new item can be added to the collection.
Explicit interface implemetation Private property IEditableCollectionViewCanCancelEditGets a value that indicates whether the collection view can discard pending changes and restore the original values of an edited object.
Explicit interface implemetation Private method IEditableCollectionViewCancelEditEnds the edit transaction and, if possible, restores the original value to the item.
Explicit interface implemetation Private method IEditableCollectionViewCancelNewEnds the add transaction and discards the pending new item.
Explicit interface implemetation Private property IEditableCollectionViewCanRemoveGets a value that indicates whether an item can be removed from the collection.
Explicit interface implemetation Private method IEditableCollectionViewCommitEditEnds the edit transaction and saves the pending changes.
Explicit interface implemetation Private method IEditableCollectionViewCommitNewEnds the add transaction and saves the pending new item.
Explicit interface implemetation Private property IEditableCollectionViewCurrentAddItemGets the item that is being added during the current add transaction.
Explicit interface implemetation Private property IEditableCollectionViewCurrentEditItemGets the item in the collection that is being edited.
Explicit interface implemetation Private method IEditableCollectionViewEditItemBegins an edit transaction of the specified item.
Explicit interface implemetation Private property IEditableCollectionViewIsAddingNewGets a value that indicates whether an add transaction is in progress.
Explicit interface implemetation Private property IEditableCollectionViewIsEditingItemGets a value that indicates whether an edit transaction is in progress.
Explicit interface implemetation Private property IEditableCollectionViewNewItemPlaceholderPositionGets or sets the position of the new item placeholder in the collection view.
Explicit interface implemetation Private method IEditableCollectionViewRemoveRemoves the specified item from the collection.
Explicit interface implemetation Private method IEditableCollectionViewRemoveAtRemoves the item at the specified position from the collection.
Explicit interface implemetation Private method IEditableCollectionViewAddNewItemAddNewItemAdds the specified object to the collection.
Explicit interface implemetation Private property IEditableCollectionViewAddNewItemCanAddNewItemGets a value that indicates whether a specified object can be added to the collection.
Explicit interface implemetation Private method IEnumerableGetEnumeratorReturns an IEnumerator object that you can use to enumerate the items in the view. (Inherited from CollectionView.)
Explicit interface implemetation Private property IItemPropertiesItemPropertiesGets a collection that contains information about the properties that are available on the items in a collection.
Explicit interface implemetation Private property IListIsFixedSizeThis member supports the Windows Presentation Foundation (WPF) infrastructure and is not intended to be used directly from your code.
Explicit interface implemetation Private property IListIsReadOnlyThis member supports the Windows Presentation Foundation (WPF) infrastructure and is not intended to be used directly from your code.
Explicit interface implemetation Private event INotifyCollectionChangedCollectionChangedOccurs when the view has changed. (Inherited from CollectionView.)
Explicit interface implemetation Private event INotifyPropertyChangedPropertyChangedOccurs when a property value changes. (Inherited from CollectionView.)
Explicit interface implemetation Private method IWeakEventListenerReceiveWeakEventReceives events from the centralized event manager.
Top
Remarks

ItemCollection maintains a collection of items such as strings, objects, XML nodes, elements, and other collections. An ItemsControl uses the data in the ItemCollection to generate its content. If you want to aggregate more than one collection, assign a CompositeCollection to the ItemsSource property of your ItemsControl.

Use either the Items property or the ItemsSource property to specify the collection that should be used to generate the content of your ItemsControl. When the ItemsSource property is set, the Items collection will be made read-only and fixed-size. This means that if you are using the ItemsSource property, you cannot add, delete, or change items in the ItemCollection directly.

ItemCollection is a CollectionView and therefore provides collection view functionalities, such as sorting, grouping, and filtering. For more information, see SortDescriptions, Filter, and GroupDescriptions.

For more information about collection views, see Data Binding Overview.

Examples

If you have an ItemsControl, such as a ListBox that has content, you can use the Items property to access the ItemCollection, which is a view. Because it is a view, you can then use the view-related functionalities such as sorting, filtering, and grouping. Note that when ItemsSource is set, the view operations delegate to the view over the ItemsSource collection. Therefore, the ItemCollection supports sorting, filtering, and grouping only if the delegated view supported them.

The following example shows how to sort the content of a ListBox named myListBox. In this example, Content is the name of the property to sort by.

			myListBox.Items.SortDescriptions.Add(New SortDescription("Content", ListSortDirection.Descending))
myListBox.Items.SortDescriptions.Add(
    new SortDescription("Content", ListSortDirection.Descending));

When you do this, the view might or might not be the default view, depending on how the data is set up on your ItemsControl. For example, when the ItemsSource property is bound to a CollectionViewSource, the view that you obtain using the Items property is not the default view.

If your ItemsControl is bound (you are using the ItemsSource property), then you can do the following to get the default view:

		Private myView As CollectionView


...


			myView = CType(CollectionViewSource.GetDefaultView(myItemsControl.ItemsSource), CollectionView)
CollectionView myView;


...


myView = (CollectionView)CollectionViewSource.GetDefaultView(myItemsControl.ItemsSource);

Alternately, you can specify filtering, sorting, and grouping criteria in XAML using  CollectionViewSource.

Version Information

.NET Framework

Supported in: 4.5.3, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Platforms

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.