IEditableCollectionView 介面

定義

定義 CollectionView 實作的方法和屬性,以提供集合編輯功能。Defines methods and properties that a CollectionView implements to provide editing capabilities to a collection.

public interface class IEditableCollectionView
public interface IEditableCollectionView
type IEditableCollectionView = interface
Public Interface IEditableCollectionView
衍生

範例

下列範例顯示如何使用所定義IEditableCollectionView的方法, 將專案加入至集合。The following example shows how to add an item to a collection by using methods that are defined by IEditableCollectionView. 此應用程式會顯示銷售專案清單, 並提供使用者新增、編輯或移除專案的選項。This application displays a list of items for sale and gives the user the option of adding, editing, or removing an item. 當使用者新增或編輯專案時, 表單會提示使用者輸入新專案。When the user adds or edits an item, a form prompts the user to enter a new item. 如果使用者提交表單, 該專案就會認可至該集合。If the user submits the form, the item is committed to the collection. 如果使用者取消表單, 則會捨棄專案。If the user cancels the form, the item is discarded. 如需完整範例, 請參閱使用 IEditableCollectionView 來變更集合範例For the entire sample, see Changing a Collection by Using IEditableCollectionView Sample.

IEditableCollectionView editableCollectionView = 
    itemsControl.Items as IEditableCollectionView; 

if (!editableCollectionView.CanAddNew)
{
    MessageBox.Show("You cannot add items to the list.");
    return;
}

// Create a window that prompts the user to enter a new
// item to sell.
ChangeItemWindow win = new ChangeItemWindow();

//Create a new item to be added to the collection.
win.DataContext = editableCollectionView.AddNew();

// If the user submits the new item, commit the new
// object to the collection.  If the user cancels 
// adding the new item, discard the new item.
if ((bool)win.ShowDialog())
{
    editableCollectionView.CommitNew();
}
else
{
    editableCollectionView.CancelNew();
}

Dim editableCollectionView As IEditableCollectionView = TryCast(itemsControl.Items, IEditableCollectionView)

If Not editableCollectionView.CanAddNew Then
    MessageBox.Show("You cannot add items to the list.")
    Return
End If

' Create a window that prompts the user to enter a new
' item to sell.
Dim win As New ChangeItemWindow()

'Create a new item to be added to the collection.
win.DataContext = editableCollectionView.AddNew()

' If the user submits the new item, commit the new
' object to the collection.  If the user cancels 
' adding the new item, discard the new item.
If CBool(win.ShowDialog()) Then
    editableCollectionView.CommitNew()
Else
    editableCollectionView.CancelNew()
End If

備註

當集合視圖IEditableCollectionView執行介面時, 不論集合的類型為何, 您都可以使用IEditableCollectionView公開的方法和屬性, 直接變更基礎集合 (如果允許變更的話)。When a collection view implements the IEditableCollectionView interface, you can directly change the underlying collection, if it allows changes to be made, by using the methods and properties that IEditableCollectionView exposes, regardless of the collection's type.

類型ItemCollectionBindingListCollectionViewCollectionView是隨附于的,其繼承自。Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) ListCollectionViewThe types ItemCollection, BindingListCollectionView, and ListCollectionView are the types that ship with Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) that inherit from CollectionView. 這些類型也會執行IEditableCollectionView, 因此您可以編輯使用其中一種類型的集合。These types also implement the IEditableCollectionView, so you can edit a collection that uses one of those types. ItemCollection特別是, 通常會使用, 因為ItemsControl.Items屬性ItemCollection是。ItemCollection, in particular, is often used because the ItemsControl.Items property is an ItemCollection.

屬性

CanAddNew

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

CanCancelEdit

取得值,這個值表示集合檢視是否可以捨棄暫止的變更,並還原已編輯物件的原始值。Gets a value that indicates whether the collection view can discard pending changes and restore the original values of an edited object.

CanRemove

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

CurrentAddItem

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

CurrentEditItem

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

IsAddingNew

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

IsEditingItem

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

NewItemPlaceholderPosition

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

方法

AddNew()

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

CancelEdit()

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

CancelNew()

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

CommitEdit()

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

CommitNew()

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

EditItem(Object)

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

Remove(Object)

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

RemoveAt(Int32)

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

適用於