Επεξεργασία

Κοινή χρήση μέσω


IEditableCollectionView Interface

Definition

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
Derived

Examples

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. 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

Remarks

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.

The types ItemCollection, BindingListCollectionView, and ListCollectionView are the types that ship with Windows Presentation Foundation (WPF) that inherit from CollectionView. These types also implement the IEditableCollectionView, so you can edit a collection that uses one of those types. ItemCollection, in particular, is often used because the ItemsControl.Items property is an ItemCollection.

Properties

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.

Methods

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.

Applies to