ItemCollection ItemCollection ItemCollection ItemCollection Class

Definition

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

public ref class ItemCollection sealed : System::Windows::Data::CollectionView, System::Collections::IList, System::ComponentModel::ICollectionViewLiveShaping, System::ComponentModel::IEditableCollectionView, System::ComponentModel::IEditableCollectionViewAddNewItem, System::ComponentModel::IItemProperties, System::Windows::IWeakEventListener
[System.Windows.Localizability(System.Windows.LocalizationCategory.Ignore)]
public sealed class ItemCollection : System.Windows.Data.CollectionView, System.Collections.IList, System.ComponentModel.ICollectionViewLiveShaping, System.ComponentModel.IEditableCollectionView, System.ComponentModel.IEditableCollectionViewAddNewItem, System.ComponentModel.IItemProperties, System.Windows.IWeakEventListener
type ItemCollection = class
    inherit CollectionView
    interface IList
    interface ICollection
    interface IEnumerable
    interface IEditableCollectionViewAddNewItem
    interface IEditableCollectionView
    interface ICollectionViewLiveShaping
    interface IItemProperties
    interface IWeakEventListener
Public NotInheritable Class ItemCollection
Inherits CollectionView
Implements ICollectionViewLiveShaping, IEditableCollectionView, IEditableCollectionViewAddNewItem, IItemProperties, IList, IWeakEventListener
Inheritance
Attributes
Implements

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:

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

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

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.

Properties

AllowsCrossThreadChanges AllowsCrossThreadChanges AllowsCrossThreadChanges AllowsCrossThreadChanges

Gets a value that indicates whether a thread other than the one that created the CollectionView can change the SourceCollection.

(Inherited from CollectionView)
CanChangeLiveFiltering CanChangeLiveFiltering CanChangeLiveFiltering CanChangeLiveFiltering

Gets a value that indicates whether the collection view supports turning filtering data in real time on or off.

CanChangeLiveGrouping CanChangeLiveGrouping CanChangeLiveGrouping CanChangeLiveGrouping

Gets a value that indicates whether the collection view supports turning grouping data in real time on or off.

CanChangeLiveSorting CanChangeLiveSorting CanChangeLiveSorting CanChangeLiveSorting

Gets a value that indicates whether the collection view supports turning sorting data in real time on or off.

CanFilter CanFilter CanFilter CanFilter

Gets a value that indicates whether this collection view supports filtering.

CanGroup CanGroup CanGroup CanGroup

Gets a value that indicates whether this collection view supports grouping.

CanSort CanSort CanSort CanSort

Gets a value that indicates whether this collection view supports sorting.

Comparer Comparer Comparer Comparer

Returns an object that you can use to compare items in the view.

(Inherited from CollectionView)
Count Count Count Count

Gets the number of records in the collection.

Culture Culture Culture Culture

Gets or sets the culture information to use during sorting.

(Inherited from CollectionView)
CurrentItem CurrentItem CurrentItem CurrentItem

Gets the current item in the view.

CurrentPosition CurrentPosition CurrentPosition CurrentPosition

Gets the ordinal position of the current item within the view.

Dispatcher Dispatcher Dispatcher Dispatcher

Gets the Dispatcher this DispatcherObject is associated with.

(Inherited from DispatcherObject)
Filter Filter Filter Filter

Gets or sets a callback used to determine if an item is suitable for inclusion in the view.

GroupDescriptions GroupDescriptions GroupDescriptions GroupDescriptions

Gets a collection of GroupDescription objects that defines how to group the items.

Groups Groups Groups Groups

Gets the top-level groups that are constructed according to the GroupDescriptions.

IsCurrentAfterLast IsCurrentAfterLast IsCurrentAfterLast IsCurrentAfterLast

Gets a value that indicates whether the current item of the view is beyond the end of the collection.

IsCurrentBeforeFirst IsCurrentBeforeFirst IsCurrentBeforeFirst IsCurrentBeforeFirst

Gets a value that indicates whether the current item of the view is beyond the beginning of the collection.

IsCurrentInSync IsCurrentInSync IsCurrentInSync IsCurrentInSync

Gets a value that indicates whether the CurrentItem is at the CurrentPosition.

(Inherited from CollectionView)
IsDynamic IsDynamic IsDynamic IsDynamic

Gets a value that indicates whether the underlying collection provides change notifications.

(Inherited from CollectionView)
IsEmpty IsEmpty IsEmpty IsEmpty

Gets a value that indicates whether the resulting (filtered) view is empty.

IsInUse IsInUse IsInUse IsInUse

Gets a value that indicates whether any object is subscribing to the events of this CollectionView.

(Inherited from CollectionView)
IsLiveFiltering IsLiveFiltering IsLiveFiltering IsLiveFiltering

Gets or sets a value that indicates whether filtering data in real time is enabled.

IsLiveGrouping IsLiveGrouping IsLiveGrouping IsLiveGrouping

Gets or sets a value that indicates whether grouping data in real time is enabled.

IsLiveSorting IsLiveSorting IsLiveSorting IsLiveSorting

Gets or sets a value that indicates whether sorting in real time is enabled.

IsRefreshDeferred IsRefreshDeferred IsRefreshDeferred IsRefreshDeferred

Gets a value that indicates whether there is an outstanding DeferRefresh() in use.

(Inherited from CollectionView)
Item[Int32] Item[Int32] Item[Int32] Item[Int32]

Gets or sets the item at the given zero-based index.

LiveFilteringProperties LiveFilteringProperties LiveFilteringProperties LiveFilteringProperties

Gets a collection of strings that specify the properties that participate in filtering data in real time.

LiveGroupingProperties LiveGroupingProperties LiveGroupingProperties LiveGroupingProperties

Gets a collection of strings that specify the properties that participate in grouping data in real time.

LiveSortingProperties LiveSortingProperties LiveSortingProperties LiveSortingProperties

Gets a collection of strings that specify the properties that participate in sorting data in real time.

NeedsRefresh NeedsRefresh NeedsRefresh NeedsRefresh

Gets a value that indicates whether the collection needs to be refreshed.

SortDescriptions SortDescriptions SortDescriptions SortDescriptions

Gets a collection of SortDescription objects that describe how the items in the collection are sorted in the view.

SourceCollection SourceCollection SourceCollection SourceCollection

Gets the unsorted and unfiltered collection that underlies this collection view.

UpdatedOutsideDispatcher UpdatedOutsideDispatcher UpdatedOutsideDispatcher UpdatedOutsideDispatcher

Gets a value that indicates whether it has been necessary to update the change log because a CollectionChanged notification has been received on a different thread without first entering the user interface (UI) thread dispatcher.

(Inherited from CollectionView)

Methods

Add(Object) Add(Object) Add(Object) Add(Object)

Adds an item to the ItemCollection.

CheckAccess() CheckAccess() CheckAccess() CheckAccess()

Determines whether the calling thread has access to this DispatcherObject.

(Inherited from DispatcherObject)
Clear() Clear() Clear() Clear()

Clears the collection and releases the references on all items currently in the collection.

ClearChangeLog() ClearChangeLog() ClearChangeLog() ClearChangeLog()

Clears any pending changes from the change log.

(Inherited from CollectionView)
ClearPendingChanges() ClearPendingChanges() ClearPendingChanges() ClearPendingChanges()

Clears unprocessed changed to the collection.

(Inherited from CollectionView)
Contains(Object) Contains(Object) Contains(Object) Contains(Object)

Returns a value that indicates whether the specified item is in this view.

CopyTo(Array, Int32) CopyTo(Array, Int32) CopyTo(Array, Int32) CopyTo(Array, Int32)

Copies the elements of the collection to an array, starting at a particular array index.

DeferRefresh() DeferRefresh() DeferRefresh() DeferRefresh()

Enters a defer cycle that you can use to merge changes to the view and delay automatic refresh.

DetachFromSourceCollection() DetachFromSourceCollection() DetachFromSourceCollection() DetachFromSourceCollection()

Removes the reference to the underlying collection from the CollectionView.

(Inherited from CollectionView)
Equals(Object) Equals(Object) Equals(Object) Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetEnumerator() GetEnumerator() GetEnumerator() GetEnumerator()

Returns an object that you can use to enumerate the items in the view.

(Inherited from CollectionView)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetItemAt(Int32) GetItemAt(Int32) GetItemAt(Int32) GetItemAt(Int32)

Returns the item at the specified zero-based index in this view.

GetType() GetType() GetType() GetType()

Gets the Type of the current instance.

(Inherited from Object)
IndexOf(Object) IndexOf(Object) IndexOf(Object) IndexOf(Object)

Returns the index in this collection where the specified item is located.

Insert(Int32, Object) Insert(Int32, Object) Insert(Int32, Object) Insert(Int32, Object)

Inserts an element into the collection at the specified index.

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
MoveCurrentTo(Object) MoveCurrentTo(Object) MoveCurrentTo(Object) MoveCurrentTo(Object)

Sets the specified item in the collection as the CurrentItem.

MoveCurrentToFirst() MoveCurrentToFirst() MoveCurrentToFirst() MoveCurrentToFirst()

Sets the first item in the view as the CurrentItem.

MoveCurrentToLast() MoveCurrentToLast() MoveCurrentToLast() MoveCurrentToLast()

Sets the last item in the view as the CurrentItem.

MoveCurrentToNext() MoveCurrentToNext() MoveCurrentToNext() MoveCurrentToNext()

Sets the item after the CurrentItem in the view as the CurrentItem.

MoveCurrentToPosition(Int32) MoveCurrentToPosition(Int32) MoveCurrentToPosition(Int32) MoveCurrentToPosition(Int32)

Sets the item at the specified index to be the CurrentItem in the view.

MoveCurrentToPrevious() MoveCurrentToPrevious() MoveCurrentToPrevious() MoveCurrentToPrevious()

Sets the item before the CurrentItem in the view as the CurrentItem.

OKToChangeCurrent() OKToChangeCurrent() OKToChangeCurrent() OKToChangeCurrent()

Returns a value that indicates whether the view can change which item is the CurrentItem.

(Inherited from CollectionView)
OnAllowsCrossThreadChangesChanged() OnAllowsCrossThreadChangesChanged() OnAllowsCrossThreadChangesChanged() OnAllowsCrossThreadChangesChanged()

Occurs when the AllowsCrossThreadChanges property changes.

(Inherited from CollectionView)
OnBeginChangeLogging(NotifyCollectionChangedEventArgs) OnBeginChangeLogging(NotifyCollectionChangedEventArgs) OnBeginChangeLogging(NotifyCollectionChangedEventArgs) OnBeginChangeLogging(NotifyCollectionChangedEventArgs)

Called by the base class to notify the derived class that an CollectionChanged event has been posted to the message queue.

(Inherited from CollectionView)
OnCollectionChanged(NotifyCollectionChangedEventArgs) OnCollectionChanged(NotifyCollectionChangedEventArgs) OnCollectionChanged(NotifyCollectionChangedEventArgs) OnCollectionChanged(NotifyCollectionChangedEventArgs)

Raises the CollectionChanged event.

(Inherited from CollectionView)
OnCollectionChanged(Object, NotifyCollectionChangedEventArgs) OnCollectionChanged(Object, NotifyCollectionChangedEventArgs) OnCollectionChanged(Object, NotifyCollectionChangedEventArgs) OnCollectionChanged(Object, NotifyCollectionChangedEventArgs)

Raises the CollectionChanged event.

(Inherited from CollectionView)
OnCurrentChanged() OnCurrentChanged() OnCurrentChanged() OnCurrentChanged()

Raises the CurrentChanged event.

(Inherited from CollectionView)
OnCurrentChanging() OnCurrentChanging() OnCurrentChanging() OnCurrentChanging()

Raises a CurrentChanging event that is not cancelable.

(Inherited from CollectionView)
OnCurrentChanging(CurrentChangingEventArgs) OnCurrentChanging(CurrentChangingEventArgs) OnCurrentChanging(CurrentChangingEventArgs) OnCurrentChanging(CurrentChangingEventArgs)

Raises the CurrentChanging event with the specified arguments.

(Inherited from CollectionView)
OnPropertyChanged(PropertyChangedEventArgs) OnPropertyChanged(PropertyChangedEventArgs) OnPropertyChanged(PropertyChangedEventArgs) OnPropertyChanged(PropertyChangedEventArgs)

Raises the PropertyChanged event using the specified arguments.

(Inherited from CollectionView)
PassesFilter(Object) PassesFilter(Object) PassesFilter(Object) PassesFilter(Object)

Returns a value that indicates whether the specified item belongs to this view.

ProcessCollectionChanged(NotifyCollectionChangedEventArgs) ProcessCollectionChanged(NotifyCollectionChangedEventArgs) ProcessCollectionChanged(NotifyCollectionChangedEventArgs) ProcessCollectionChanged(NotifyCollectionChangedEventArgs)

When overridden in a derived class, processes a single change on the UI thread.

(Inherited from CollectionView)
ProcessPendingChanges() ProcessPendingChanges() ProcessPendingChanges() ProcessPendingChanges()

Ensures that all pending changes to the collection have been committed.

(Inherited from CollectionView)
Refresh() Refresh() Refresh() Refresh()

Re-creates the view.

(Inherited from CollectionView)
RefreshOrDefer() RefreshOrDefer() RefreshOrDefer() RefreshOrDefer()

Refreshes the view or specifies that the view needs to be refreshed when the defer cycle completes.

(Inherited from CollectionView)
RefreshOverride() RefreshOverride() RefreshOverride() RefreshOverride()

Re-creates the view.

(Inherited from CollectionView)
Remove(Object) Remove(Object) Remove(Object) Remove(Object)

Removes the specified item reference from the collection or view.

RemoveAt(Int32) RemoveAt(Int32) RemoveAt(Int32) RemoveAt(Int32)

Removes the item at the specified index of the collection or view.

SetCurrent(Object, Int32) SetCurrent(Object, Int32) SetCurrent(Object, Int32) SetCurrent(Object, Int32)

Sets the specified item and index as the values of the CurrentItem and CurrentPosition properties.

(Inherited from CollectionView)
SetCurrent(Object, Int32, Int32) SetCurrent(Object, Int32, Int32) SetCurrent(Object, Int32, Int32) SetCurrent(Object, Int32, Int32)

Sets the specified item and index as the values of the CurrentItem and CurrentPosition properties. This method can be called from a constructor of a derived class.

(Inherited from CollectionView)
ToString() ToString() ToString() ToString()

Returns a string that represents the current object.

(Inherited from Object)
VerifyAccess() VerifyAccess() VerifyAccess() VerifyAccess()

Enforces that the calling thread has access to this DispatcherObject.

(Inherited from DispatcherObject)

Explicit Interface Implementations

ICollection.IsSynchronized ICollection.IsSynchronized ICollection.IsSynchronized ICollection.IsSynchronized

This member supports the Windows Presentation Foundation (WPF) infrastructure and is not intended to be used directly from your code.

ICollection.SyncRoot ICollection.SyncRoot ICollection.SyncRoot ICollection.SyncRoot

This member supports the Windows Presentation Foundation (WPF) infrastructure and is not intended to be used directly from your code.

IEditableCollectionView.AddNew() IEditableCollectionView.AddNew() IEditableCollectionView.AddNew() IEditableCollectionView.AddNew()

Adds a new item to the collection.

IEditableCollectionView.CanAddNew IEditableCollectionView.CanAddNew IEditableCollectionView.CanAddNew IEditableCollectionView.CanAddNew

Gets a value that indicates whether a new item can be added to the collection.

IEditableCollectionView.CanCancelEdit IEditableCollectionView.CanCancelEdit IEditableCollectionView.CanCancelEdit IEditableCollectionView.CanCancelEdit

Gets a value that indicates whether the collection view can discard pending changes and restore the original values of an edited object.

IEditableCollectionView.CancelEdit() IEditableCollectionView.CancelEdit() IEditableCollectionView.CancelEdit() IEditableCollectionView.CancelEdit()

Ends the edit transaction and, if possible, restores the original value to the item.

IEditableCollectionView.CancelNew() IEditableCollectionView.CancelNew() IEditableCollectionView.CancelNew() IEditableCollectionView.CancelNew()

Ends the add transaction and discards the pending new item.

IEditableCollectionView.CanRemove IEditableCollectionView.CanRemove IEditableCollectionView.CanRemove IEditableCollectionView.CanRemove

Gets a value that indicates whether an item can be removed from the collection.

IEditableCollectionView.CommitEdit() IEditableCollectionView.CommitEdit() IEditableCollectionView.CommitEdit() IEditableCollectionView.CommitEdit()

Ends the edit transaction and saves the pending changes.

IEditableCollectionView.CommitNew() IEditableCollectionView.CommitNew() IEditableCollectionView.CommitNew() IEditableCollectionView.CommitNew()

Ends the add transaction and saves the pending new item.

IEditableCollectionView.CurrentAddItem IEditableCollectionView.CurrentAddItem IEditableCollectionView.CurrentAddItem IEditableCollectionView.CurrentAddItem

Gets the item that is being added during the current add transaction.

IEditableCollectionView.CurrentEditItem IEditableCollectionView.CurrentEditItem IEditableCollectionView.CurrentEditItem IEditableCollectionView.CurrentEditItem

Gets the item in the collection that is being edited.

IEditableCollectionView.EditItem(Object) IEditableCollectionView.EditItem(Object) IEditableCollectionView.EditItem(Object) IEditableCollectionView.EditItem(Object)

Begins an edit transaction of the specified item.

IEditableCollectionView.IsAddingNew IEditableCollectionView.IsAddingNew IEditableCollectionView.IsAddingNew IEditableCollectionView.IsAddingNew

Gets a value that indicates whether an add transaction is in progress.

IEditableCollectionView.IsEditingItem IEditableCollectionView.IsEditingItem IEditableCollectionView.IsEditingItem IEditableCollectionView.IsEditingItem

Gets a value that indicates whether an edit transaction is in progress.

IEditableCollectionView.NewItemPlaceholderPosition IEditableCollectionView.NewItemPlaceholderPosition IEditableCollectionView.NewItemPlaceholderPosition IEditableCollectionView.NewItemPlaceholderPosition

Gets or sets the position of the new item placeholder in the collection view.

IEditableCollectionView.Remove(Object) IEditableCollectionView.Remove(Object) IEditableCollectionView.Remove(Object) IEditableCollectionView.Remove(Object)

Removes the specified item from the collection.

IEditableCollectionView.RemoveAt(Int32) IEditableCollectionView.RemoveAt(Int32) IEditableCollectionView.RemoveAt(Int32) IEditableCollectionView.RemoveAt(Int32)

Removes the item at the specified position from the collection.

IEditableCollectionViewAddNewItem.AddNewItem(Object) IEditableCollectionViewAddNewItem.AddNewItem(Object) IEditableCollectionViewAddNewItem.AddNewItem(Object) IEditableCollectionViewAddNewItem.AddNewItem(Object)

Adds the specified object to the collection.

IEditableCollectionViewAddNewItem.CanAddNewItem IEditableCollectionViewAddNewItem.CanAddNewItem IEditableCollectionViewAddNewItem.CanAddNewItem IEditableCollectionViewAddNewItem.CanAddNewItem

Gets a value that indicates whether a specified object can be added to the collection.

IEnumerable.GetEnumerator() IEnumerable.GetEnumerator() IEnumerable.GetEnumerator() IEnumerable.GetEnumerator()

Returns an IEnumerator object that you can use to enumerate the items in the view.

(Inherited from CollectionView)
IItemProperties.ItemProperties IItemProperties.ItemProperties IItemProperties.ItemProperties IItemProperties.ItemProperties

Gets a collection that contains information about the properties that are available on the items in a collection.

IList.IsFixedSize IList.IsFixedSize IList.IsFixedSize IList.IsFixedSize

This member supports the Windows Presentation Foundation (WPF) infrastructure and is not intended to be used directly from your code.

IList.IsReadOnly IList.IsReadOnly IList.IsReadOnly IList.IsReadOnly

This member supports the Windows Presentation Foundation (WPF) infrastructure and is not intended to be used directly from your code.

IWeakEventListener.ReceiveWeakEvent(Type, Object, EventArgs) IWeakEventListener.ReceiveWeakEvent(Type, Object, EventArgs) IWeakEventListener.ReceiveWeakEvent(Type, Object, EventArgs) IWeakEventListener.ReceiveWeakEvent(Type, Object, EventArgs)

Receives events from the centralized event manager.

Events

CollectionChanged CollectionChanged CollectionChanged CollectionChanged

Occurs when the view has changed.

(Inherited from CollectionView)
CurrentChanged CurrentChanged CurrentChanged CurrentChanged

Occurs after the CurrentItem has changed.

(Inherited from CollectionView)
CurrentChanging CurrentChanging CurrentChanging CurrentChanging

Occurs when the CurrentItem is changing.

(Inherited from CollectionView)
INotifyCollectionChanged.CollectionChanged INotifyCollectionChanged.CollectionChanged INotifyCollectionChanged.CollectionChanged INotifyCollectionChanged.CollectionChanged Inherited from CollectionView
INotifyPropertyChanged.PropertyChanged INotifyPropertyChanged.PropertyChanged INotifyPropertyChanged.PropertyChanged INotifyPropertyChanged.PropertyChanged Inherited from CollectionView
PropertyChanged PropertyChanged PropertyChanged PropertyChanged

Occurs when a property value has changed.

(Inherited from CollectionView)

Extension Methods

Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable)

Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable)

Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable)

Enables parallelization of a query.

AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable)

Converts an IEnumerable to an IQueryable.

Applies to

See Also