IEditableCollectionView 介面

定義

定義 CollectionView 實作的方法和屬性,以提供集合的編輯功能。

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

範例

下列範例示範如何使用 所 IEditableCollectionView 定義的方法,將專案加入至集合。 此應用程式會顯示要銷售的專案清單,並提供使用者新增、編輯或移除專案的選項。 當使用者新增或編輯專案時,表單會提示使用者輸入新專案。 如果使用者提交表單,專案就會認可至集合。 如果使用者取消表單,則會捨棄專案。 如需整個範例,請參閱 使用 IEditableCollectionView 範例變更集合

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 ,不論集合的類型為何,都可以直接變更基礎集合。

ItemCollection 別 、 BindingListCollectionViewListCollectionView 是繼承自 CollectionView Windows Presentation Foundation (WPF) 隨附的類型。 這些類型也會實作 IEditableCollectionView ,因此您可以編輯使用其中一種類型的集合。 ItemCollection特別是,通常會使用 ,因為 ItemsControl.Items 屬性是 ItemCollection

屬性

CanAddNew

取得值,這個值表示新項目是否可以加入至此集合。

CanCancelEdit

取得值,這個值表示集合檢視是否可以捨棄暫止的變更,並還原已編輯物件的原始值。

CanRemove

取得值,這個值表示是否可以從集合中移除某個項目。

CurrentAddItem

取得要在目前加入異動期間加入的項目。

CurrentEditItem

取得集合內將被編輯的項目。

IsAddingNew

取得值,這個值表示加入異動是否正在進行中。

IsEditingItem

取得值,這個值表示編輯異動是否正在進行中。

NewItemPlaceholderPosition

取得或設定集合檢視中新項目預留位置 (Placeholder) 的位置 (Position)。

方法

AddNew()

將新項目加入至集合中。

CancelEdit()

結束編輯異動,並在可能情況下還原項目的原始值。

CancelNew()

結束加入異動並捨棄暫止的新項目。

CommitEdit()

結束編輯異動並儲存暫止的變更。

CommitNew()

結束加入異動並儲存暫止的新項目。

EditItem(Object)

開始所指定項目的編輯異動。

Remove(Object)

從集合中移除指定的項目。

RemoveAt(Int32)

從集合中移除位於指定之位置的項目。

適用於