Item​Collection Class

Definition

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

[System.Windows.Localizability(System.Windows.LocalizationCategory.Ignore)]
public sealed class ItemCollection : System.Windows.Data.CollectionView, System.Collections.IList, System.ComponentModel.ICollectionViewLiveShaping, System.ComponentModel.IEditableCollectionViewAddNewItem, System.ComponentModel.IItemProperties, System.Windows.IWeakEventListener
Inheritance
Attributes
Implements

Inherited Members

System.Object

System.Windows.Data.CollectionView

System.Windows.Threading.DispatcherObject

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

Can​Change​Live​Filtering

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

Can​Change​Live​Grouping

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

Can​Change​Live​Sorting

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

Can​Filter

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

Can​Group

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

Can​Sort

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

Count

Gets the number of records in the collection.

Current​Item

Gets the current item in the view.

Current​Position

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

Filter

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

Group​Descriptions

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

Groups

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

Is​Current​After​Last

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

Is​Current​Before​First

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

Is​Empty

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

Is​Live​Filtering

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

Is​Live​Grouping

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

Is​Live​Sorting

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

Item(​Int32)

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

Live​Filtering​Properties

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

Live​Grouping​Properties

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

Live​Sorting​Properties

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

Needs​Refresh

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

Sort​Descriptions

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

Source​Collection

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

Methods

Add(​Object)

Adds an item to the ItemCollection.

Clear()

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

Contains(​Object)

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

Copy​To(​Array, ​Int32)

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

Defer​Refresh()

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

Get​Enumerator()
Get​Item​At(​Int32)

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

Index​Of(​Object)

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

Insert(​Int32, ​Object)

Inserts an element into the collection at the specified index.

Move​Current​To(​Object)

Sets the specified item in the collection as the CurrentItem.

Move​Current​To​First()

Sets the first item in the view as the CurrentItem.

Move​Current​To​Last()

Sets the last item in the view as the CurrentItem.

Move​Current​To​Next()

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

Move​Current​To​Position(​Int32)

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

Move​Current​To​Previous()

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

Passes​Filter(​Object)

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

Refresh​Override()
Remove(​Object)

Removes the specified item reference from the collection or view.

Remove​At(​Int32)

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

Explicit Interface Implementations

I​Collection.​Is​Synchronized

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

I​Collection.​Sync​Root

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

I​List.​Is​Fixed​Size

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

I​List.​Is​Read​Only

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

I​Editable​Collection​View.​Add​New()

Adds a new item to the collection.

I​Editable​Collection​View.​Can​Add​New

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

I​Editable​Collection​View.​Can​Cancel​Edit

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

I​Editable​Collection​View.​Cancel​Edit()

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

I​Editable​Collection​View.​Cancel​New()

Ends the add transaction and discards the pending new item.

I​Editable​Collection​View.​Can​Remove

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

I​Editable​Collection​View.​Commit​Edit()

Ends the edit transaction and saves the pending changes.

I​Editable​Collection​View.​Commit​New()

Ends the add transaction and saves the pending new item.

I​Editable​Collection​View.​Current​Add​Item

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

I​Editable​Collection​View.​Current​Edit​Item

Gets the item in the collection that is being edited.

I​Editable​Collection​View.​Edit​Item(​Object)

Begins an edit transaction of the specified item.

I​Editable​Collection​View.​Is​Adding​New

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

I​Editable​Collection​View.​Is​Editing​Item

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

I​Editable​Collection​View.​New​Item​Placeholder​Position

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

I​Editable​Collection​View.​Remove(​Object)

Removes the specified item from the collection.

I​Editable​Collection​View.​Remove​At(​Int32)

Removes the item at the specified position from the collection.

I​Editable​Collection​View​Add​New​Item.​Add​New​Item(​Object)

Adds the specified object to the collection.

I​Editable​Collection​View​Add​New​Item.​Can​Add​New​Item

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

I​Item​Properties.​Item​Properties

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

I​Weak​Event​Listener.​Receive​Weak​Event(​Type, ​Object, ​Event​Args)

Receives events from the centralized event manager.

Extension Methods

Cast<TResult>(IEnumerable)

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

OfType<TResult>(IEnumerable)

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

AsParallel(IEnumerable)

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

AsQueryable(IEnumerable)

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