ItemCollection 類別

定義

保存項目清單,這些項目會構成 ItemsControl 的內容。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::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.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, IEditableCollectionViewAddNewItem, IItemProperties, IList, IWeakEventListener
繼承
屬性
實作

範例

如果您有ItemsControl(例如ListBox具有內容的), 您可以使用Items屬性來存取ItemCollection, 這是一個視圖。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. 請注意, 當設定 ItemsSource 時, view 作業會委派至 ItemsSource 集合的 view。Note that when ItemsSource is set, the view operations delegate to the view over the ItemsSource collection. 因此, 只有在委派的視圖支援時, ItemCollection 才支援排序、篩選和群組。Therefore, the ItemCollection supports sorting, filtering, and grouping only if the delegated view supported them.

下列範例顯示如何排序ListBox名為myListBox的內容。The following example shows how to sort the content of a ListBox named myListBox. 在此範例中Content , 是要排序的屬性名稱。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))

當您這麼做時, 視上ItemsControl的資料設定方式而定, 此視圖不一定是預設的視圖。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. 例如, 當ItemsSource屬性系結CollectionViewSource至時, 您使用Items屬性取得的視圖就不是預設的視圖。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.

如果您ItemsControl的已系結 (您ItemsSource使用屬性), 則您可以執行下列動作來取得預設的 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)

或者, 您可以使用XAMLXAML CollectionViewSource, 在中指定篩選、排序和分組條件。Alternately, you can specify filtering, sorting, and grouping criteria in XAMLXAML using CollectionViewSource.

備註

ItemCollection維護專案的集合, 例如字串、物件、 XMLXML節點、元素和其他集合。ItemCollection maintains a collection of items such as strings, objects, XMLXML nodes, elements, and other collections. 會使用中的資料來產生其內容。 ItemCollection ItemsControlAn ItemsControl uses the data in the ItemCollection to generate its content. 如果您想要匯總一個以上的集合, 請將CompositeCollection指派ItemsSource給的屬性ItemsControlIf you want to aggregate more than one collection, assign a CompositeCollection to the ItemsSource property of your ItemsControl.

您可以Items使用屬性ItemsSource或屬性來指定應該用來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. 設定屬性時, 會將Items集合設為唯讀和固定大小。 ItemsSourceWhen the ItemsSource property is set, the Items collection will be made read-only and fixed-size. 這表示如果您使用ItemsSource屬性, 則無法ItemCollection直接在中加入、刪除或變更專案。This means that if you are using the ItemsSource property, you cannot add, delete, or change items in the ItemCollection directly.

ItemCollectionCollectionView是, 因此會提供集合視圖功能, 例如排序、群組和篩選。ItemCollection is a CollectionView and therefore provides collection view functionalities, such as sorting, grouping, and filtering. 如需詳細資訊,請參閱SortDescriptionsFilterGroupDescriptionsFor more information, see SortDescriptions, Filter, and GroupDescriptions.

如需集合視圖的詳細資訊, 請參閱資料系結總覽For more information about collection views, see Data Binding Overview.

屬性

AllowsCrossThreadChanges

取得值,指出建立 CollectionView 的執行緒之外的執行緒是否可以變更 SourceCollectionGets a value that indicates whether a thread other than the one that created the CollectionView can change the SourceCollection.

(繼承來源 CollectionView)
CanChangeLiveFiltering

取得值,其中該值表示集合檢視是否支援即時開啟或關閉篩選資料。Gets a value that indicates whether the collection view supports turning filtering data in real time on or off.

CanChangeLiveGrouping

取得值,這個值表示這個集合檢視是否支援開啟或關閉即時群組資料功能。Gets a value that indicates whether the collection view supports turning grouping data in real time on or off.

CanChangeLiveSorting

取得值,這個值表示這個集合檢視是否支援開啟或關閉即時排序資料功能。Gets a value that indicates whether the collection view supports turning sorting data in real time on or off.

CanFilter

取得值,這個值表示此集合檢視是否支援篩選。Gets a value that indicates whether this collection view supports filtering.

CanGroup

取得值,這個值表示此集合檢視是否支援群組。Gets a value that indicates whether this collection view supports grouping.

CanSort

取得值,這個值表示此集合檢視是否支援排序。Gets a value that indicates whether this collection view supports sorting.

Comparer

傳回物件,可用來比較檢視中的項目。Returns an object that you can use to compare items in the view.

(繼承來源 CollectionView)
Count

取得集合中的記錄數目。Gets the number of records in the collection.

Culture

取得或設定排序期間要使用的文化特性 (Culture) 資訊。Gets or sets the culture information to use during sorting.

(繼承來源 CollectionView)
CurrentItem

取得檢視中的目前項目。Gets the current item in the view.

CurrentPosition

取得目前項目在檢視中的序數位置。Gets the ordinal position of the current item within the view.

Dispatcher

取得與這個 Dispatcher 關聯的 DispatcherObjectGets the Dispatcher this DispatcherObject is associated with.

(繼承來源 DispatcherObject)
Filter

取得或設定回呼,這個回呼是用來判斷項目是否適合包含在檢視中。Gets or sets a callback used to determine if an item is suitable for inclusion in the view.

GroupDescriptions

取得定義如何群組項目的 GroupDescription 物件集合。Gets a collection of GroupDescription objects that defines how to group the items.

Groups

取得根據 GroupDescriptions 建構的最上層群組。Gets the top-level groups that are constructed according to the GroupDescriptions.

IsCurrentAfterLast

取得值,這個值表示檢視的目前項目是否超過集合的結尾。Gets a value that indicates whether the current item of the view is beyond the end of the collection.

IsCurrentBeforeFirst

取得值,這個值表示檢視的目前項目是否超過集合的開頭。Gets a value that indicates whether the current item of the view is beyond the beginning of the collection.

IsCurrentInSync

取得值,指出 CurrentItem 是否位於 CurrentPositionGets a value that indicates whether the CurrentItem is at the CurrentPosition.

(繼承來源 CollectionView)
IsDynamic

取得值,表示基礎集合是否提供變更通知。Gets a value that indicates whether the underlying collection provides change notifications.

(繼承來源 CollectionView)
IsEmpty

取得值,這個值表示產生的 (已篩選) 檢視是否為空白。Gets a value that indicates whether the resulting (filtered) view is empty.

IsInUse

取得值,指出是否有任何物件訂閱這個 CollectionView 的事件。Gets a value that indicates whether any object is subscribing to the events of this CollectionView.

(繼承來源 CollectionView)
IsLiveFiltering

取得或設定值,指出是否已啟用即時篩選資料。Gets or sets a value that indicates whether filtering data in real time is enabled.

IsLiveGrouping

取得或設定值,指出是否已啟用即時設定資料群組。Gets or sets a value that indicates whether grouping data in real time is enabled.

IsLiveSorting

取得或設定值,指出是否已啟用即時排序。Gets or sets a value that indicates whether sorting in real time is enabled.

IsRefreshDeferred

取得值,指出是否仍然有未處理的 DeferRefresh() 在使用中。Gets a value that indicates whether there is an outstanding DeferRefresh() in use.

(繼承來源 CollectionView)
Item[Int32]

取得或設定位於指定之以零起始的索引的項目。Gets or sets the item at the given zero-based index.

LiveFilteringProperties

取得字串集合,這些字串會指定參與即時篩選資料的屬性。Gets a collection of strings that specify the properties that participate in filtering data in real time.

LiveGroupingProperties

取得字串集合,這些字串會指定參與即時篩群組資料的屬性。Gets a collection of strings that specify the properties that participate in grouping data in real time.

LiveSortingProperties

取得字串集合,這些字串會指定參與即時排序資料的屬性。Gets a collection of strings that specify the properties that participate in sorting data in real time.

NeedsRefresh

取得值,這個值表示集合是否需要重新整理。Gets a value that indicates whether the collection needs to be refreshed.

SortDescriptions

取得 SortDescription 物件的集合,這些物件描述如何在檢視中排序集合中的項目。Gets a collection of SortDescription objects that describe how the items in the collection are sorted in the view.

SourceCollection

取得位於此集合檢視下之未排序且未篩選的集合。Gets the unsorted and unfiltered collection that underlies this collection view.

UpdatedOutsideDispatcher

取得值,指出是否已有需要更新變更記錄,因為已經在另一個執行緒上接收到 CollectionChanged 通知,而沒有先進入使用者介面 (UI)user interface (UI) 執行緒發送器。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 使用者介面 (UI)user interface (UI) thread dispatcher.

(繼承來源 CollectionView)

方法

Add(Object)

將項目加入至 ItemCollectionAdds an item to the ItemCollection.

CheckAccess()

判斷呼叫的執行是否可以存取這個 DispatcherObjectDetermines whether the calling thread has access to this DispatcherObject.

(繼承來源 DispatcherObject)
Clear()

清除集合並釋放目前集合中所有項目上的參考。Clears the collection and releases the references on all items currently in the collection.

ClearChangeLog()

清除變更記錄中任何暫止的變更。Clears any pending changes from the change log.

(繼承來源 CollectionView)
ClearPendingChanges()

清除集合的未處理變更。Clears unprocessed changed to the collection.

(繼承來源 CollectionView)
Contains(Object)

傳回值,這個值表示指定的項目是否位於這個檢視中。Returns a value that indicates whether the specified item is in this view.

CopyTo(Array, Int32)

從特定的陣列索引開始,將集合的項目複製到陣列。Copies the elements of the collection to an array, starting at a particular array index.

DeferRefresh()

進入延後循環,您可以用來合併變更到檢視,並且延遲自動重新整理。Enters a defer cycle that you can use to merge changes to the view and delay automatic refresh.

DetachFromSourceCollection()

CollectionView 移除基礎集合的參考。Removes the reference to the underlying collection from the CollectionView.

(繼承來源 CollectionView)
Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetEnumerator()

傳回物件,可用來列舉檢視中的項目。Returns an object that you can use to enumerate the items in the view.

(繼承來源 CollectionView)
GetHashCode()

作為預設雜湊函數。Serves as the default hash function.

(繼承來源 Object)
GetItemAt(Int32)

傳回此檢視中位於指定之以零起始的索引的項目。Returns the item at the specified zero-based index in this view.

GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
IndexOf(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.

MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
MoveCurrentTo(Object)

將集合中的指定項目設為 CurrentItemSets the specified item in the collection as the CurrentItem.

MoveCurrentToFirst()

將檢視中的第一個項目設定為 CurrentItemSets the first item in the view as the CurrentItem.

MoveCurrentToLast()

將檢視中的最後一個項目設定為 CurrentItemSets the last item in the view as the CurrentItem.

MoveCurrentToNext()

將檢視中 CurrentItem 後面的項目設定為 CurrentItemSets the item after the CurrentItem in the view as the CurrentItem.

MoveCurrentToPosition(Int32)

將指定索引處的項目設定為檢視中的 CurrentItemSets the item at the specified index to be the CurrentItem in the view.

MoveCurrentToPrevious()

將檢視中 CurrentItem 前面的項目設定為 CurrentItemSets the item before the CurrentItem in the view as the CurrentItem.

OKToChangeCurrent()

傳回值,指出檢視是否可以變更哪一個項目是 CurrentItemReturns a value that indicates whether the view can change which item is the CurrentItem.

(繼承來源 CollectionView)
OnAllowsCrossThreadChangesChanged()

發生於 AllowsCrossThreadChanges 屬性變更時。Occurs when the AllowsCrossThreadChanges property changes.

(繼承來源 CollectionView)
OnBeginChangeLogging(NotifyCollectionChangedEventArgs)

由基底類別 (Base Class) 呼叫,通知衍生類別,CollectionChanged 事件已經公佈到訊息佇列。Called by the base class to notify the derived class that an CollectionChanged event has been posted to the message queue.

(繼承來源 CollectionView)
OnCollectionChanged(NotifyCollectionChangedEventArgs)

引發 CollectionChanged 事件。Raises the CollectionChanged event.

(繼承來源 CollectionView)
OnCollectionChanged(Object, NotifyCollectionChangedEventArgs)

引發 CollectionChanged 事件。Raises the CollectionChanged event.

(繼承來源 CollectionView)
OnCurrentChanged()

引發 CurrentChanged 事件。Raises the CurrentChanged event.

(繼承來源 CollectionView)
OnCurrentChanging()

引發 CurrentChanging 事件,此事件無法取消。Raises a CurrentChanging event that is not cancelable.

(繼承來源 CollectionView)
OnCurrentChanging(CurrentChangingEventArgs)

使用指定的引數,引發 CurrentChanging 事件。Raises the CurrentChanging event with the specified arguments.

(繼承來源 CollectionView)
OnPropertyChanged(PropertyChangedEventArgs)

使用指定的引數,引發 PropertyChanged 事件。Raises the PropertyChanged event using the specified arguments.

(繼承來源 CollectionView)
PassesFilter(Object)

傳回值,這個值表示指定的項目是否屬於這個檢視。Returns a value that indicates whether the specified item belongs to this view.

ProcessCollectionChanged(NotifyCollectionChangedEventArgs)

在衍生類別中覆寫時,在 UIUI 執行緒上處理單一變更。When overridden in a derived class, processes a single change on the UIUI thread.

(繼承來源 CollectionView)
ProcessPendingChanges()

確認已認可對集合的所有暫止變更。Ensures that all pending changes to the collection have been committed.

(繼承來源 CollectionView)
Refresh()

重新建立檢視。Re-creates the view.

(繼承來源 CollectionView)
RefreshOrDefer()

重新整理檢視,或是指定檢視在延後循環完成時需要重新整理。Refreshes the view or specifies that the view needs to be refreshed when the defer cycle completes.

(繼承來源 CollectionView)
RefreshOverride()

重新建立檢視。Re-creates the view.

(繼承來源 CollectionView)
Remove(Object)

從集合或檢視中移除指定的項目參考。Removes the specified item reference from the collection or view.

RemoveAt(Int32)

從集合或檢視移除位於指定索引的項目。Removes the item at the specified index of the collection or view.

SetCurrent(Object, Int32)

將指定的項目與索引設定為 CurrentItemCurrentPosition 屬性的值。Sets the specified item and index as the values of the CurrentItem and CurrentPosition properties.

(繼承來源 CollectionView)
SetCurrent(Object, Int32, Int32)

將指定的項目與索引設定為 CurrentItemCurrentPosition 屬性的值。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.

(繼承來源 CollectionView)
ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)
VerifyAccess()

請強制執行可以存取這個 DispatcherObject 的呼叫執行緒。Enforces that the calling thread has access to this DispatcherObject.

(繼承來源 DispatcherObject)

事件

CollectionChanged

發生於檢視變更時。Occurs when the view has changed.

(繼承來源 CollectionView)
CurrentChanged

CurrentItem 已經變更之後發生。Occurs after the CurrentItem has changed.

(繼承來源 CollectionView)
CurrentChanging

發生在 CurrentItem 正在變更時。Occurs when the CurrentItem is changing.

(繼承來源 CollectionView)
PropertyChanged

在屬性值已經變更時發生。Occurs when a property value has changed.

(繼承來源 CollectionView)

明確介面實作

ICollection.IsSynchronized

這個成員支援 Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) 基礎結構,但是您不可以從程式碼直接使用它。This member supports the Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) infrastructure and is not intended to be used directly from your code.

ICollection.SyncRoot

這個成員支援 Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) 基礎結構,但是您不可以從程式碼直接使用它。This member supports the Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) infrastructure and is not intended to be used directly from your code.

IEditableCollectionView.AddNew()

將新項目加入至集合中。Adds a new item to the collection.

IEditableCollectionView.CanAddNew

取得值,這個值表示新項目是否可以加入至此集合。Gets a value that indicates whether a new item can be added to the collection.

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()

結束編輯異動,並在可能情況下還原項目的原始值。Ends the edit transaction and, if possible, restores the original value to the item.

IEditableCollectionView.CancelNew()

結束加入異動並捨棄暫止的新項目。Ends the add transaction and discards the pending new item.

IEditableCollectionView.CanRemove

取得值,這個值表示是否可以從集合中移除某個項目。Gets a value that indicates whether an item can be removed from the collection.

IEditableCollectionView.CommitEdit()

結束編輯異動並儲存暫止的變更。Ends the edit transaction and saves the pending changes.

IEditableCollectionView.CommitNew()

結束加入異動並儲存暫止的新項目。Ends the add transaction and saves the pending new item.

IEditableCollectionView.CurrentAddItem

取得要在目前加入異動期間加入的項目。Gets the item that is being added during the current add transaction.

IEditableCollectionView.CurrentEditItem

取得集合內將被編輯的項目。Gets the item in the collection that is being edited.

IEditableCollectionView.EditItem(Object)

開始指定之項目的編輯異動。Begins an edit transaction of the specified item.

IEditableCollectionView.IsAddingNew

取得值,這個值表示加入異動是否正在進行中。Gets a value that indicates whether an add transaction is in progress.

IEditableCollectionView.IsEditingItem

取得值,這個值表示編輯異動是否正在進行中。Gets a value that indicates whether an edit transaction is in progress.

IEditableCollectionView.NewItemPlaceholderPosition

取得或設定集合檢視中新項目預留位置 (Placeholder) 的位置 (Position)。Gets or sets the position of the new item placeholder in the collection view.

IEditableCollectionView.Remove(Object)

從集合中移除指定的項目。Removes the specified item from the collection.

IEditableCollectionView.RemoveAt(Int32)

從集合中移除指定位置上的項目。Removes the item at the specified position from the collection.

IEditableCollectionViewAddNewItem.AddNewItem(Object)

將指定的物件加入至集合。Adds the specified object to the collection.

IEditableCollectionViewAddNewItem.CanAddNewItem

取得值,這個值表示指定的物件是否可以加入至此集合。Gets a value that indicates whether a specified object can be added to the collection.

IEnumerable.GetEnumerator()

傳回 IEnumerator 物件,可用來列舉檢視中的項目。Returns an IEnumerator object that you can use to enumerate the items in the view.

(繼承來源 CollectionView)
IItemProperties.ItemProperties

取得集合,其中包含可在集合的項目上取得之屬性的相關資訊。Gets a collection that contains information about the properties that are available on the items in a collection.

IList.IsFixedSize

這個成員支援 Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) 基礎結構,但是您不可以從程式碼直接使用它。This member supports the Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) infrastructure and is not intended to be used directly from your code.

IList.IsReadOnly

這個成員支援 Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) 基礎結構,但是您不可以從程式碼直接使用它。This member supports the Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) infrastructure and is not intended to be used directly from your code.

INotifyCollectionChanged.CollectionChanged

發生於檢視變更時。Occurs when the view has changed.

(繼承來源 CollectionView)
INotifyPropertyChanged.PropertyChanged

當屬性值變更時發生。Occurs when a property value changes.

(繼承來源 CollectionView)
IWeakEventListener.ReceiveWeakEvent(Type, Object, EventArgs)

從集中式事件管理員接收事件。Receives events from the centralized event manager.

擴充方法

Cast<TResult>(IEnumerable)

IEnumerable 的項目轉換成指定的型別。Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable)

根據指定的型別來篩選 IEnumerable 的項目。Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable)

啟用查詢的平行化作業。Enables parallelization of a query.

AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryableConverts an IEnumerable to an IQueryable.

適用於

另請參閱