IEditableCollectionView.AddNew Methode
Definitie
Belangrijk
Bepaalde informatie heeft betrekking op een voorlopige productversie die aanzienlijk kan worden gewijzigd voordat deze wordt uitgebracht. Microsoft biedt geen enkele expliciete of impliciete garanties met betrekking tot de informatie die hier wordt verstrekt.
Hiermee voegt u een nieuw item toe aan de verzameling.
public:
System::Object ^ AddNew();
public object AddNew();
abstract member AddNew : unit -> obj
Public Function AddNew () As Object
Retouren
Het nieuwe item dat wordt toegevoegd aan de verzameling.
Voorbeelden
In het volgende voorbeeld wordt een Window gebruiker gevraagd een nieuw item toe te voegen. Vervolgens wordt aangeroepen AddNew om een nieuw object te maken en stelt u het DataContextWindow object in op dat object. Zie Changing a Collection by Using IEditableCollectionView Sample voor het volledige voorbeeld.
IEditableCollectionView editableCollectionView =
itemsControl.Items;
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()
{
//Create a new item to be added to the collection.
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
Opmerkingen
Het aanroepen AddNew begint met een transactie toevoegen. U moet de transactie voor toevoegen aanroepen CommitNew of CancelNew beƫindigen. Er wordt altijd een nieuw item weergegeven in de verzamelingsweergave. Filteren, sorteren of groeperen die op de weergave wordt toegepast, wordt toegepast op het nieuwe item wanneer CommitNew het wordt aangeroepen.