IEditableCollectionView.AddNew メソッド

定義

新しい項目をコレクションに追加します。

public:
 System::Object ^ AddNew();
public object AddNew();
abstract member AddNew : unit -> obj
Public Function AddNew () As Object

返品

コレクションに追加される新しい項目。

次の例では、ユーザーに新しい項目の追加を求める Window を作成します。 次に、AddNewを呼び出して新しいオブジェクトを作成し、DataContextWindowをそのオブジェクトに設定します。 サンプル全体については、「 IEditableCollectionView サンプルを使用したコレクションの変更」を参照してください。

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

注釈

AddNewを呼び出すと、追加トランザクションが開始されます。 追加トランザクションを終了するには、 CommitNew または CancelNew を呼び出す必要があります。 新しい項目は常にコレクション ビューに表示されます。 ビューに適用されるすべてのフィルター処理、並べ替え、またはグループ化は、 CommitNew が呼び出されたときに新しい項目に適用されます。

適用対象