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.

类型ItemCollectionBindingListCollectionViewWindows Presentation Foundation (WPF)Windows Presentation Foundation (WPF)是从继承CollectionView的类型。 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

获取或设置集合视图中新项占位符的位置。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.

适用于