IEditableCollectionView.AddNew 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
新增一個物品到收藏中。
public:
System::Object ^ AddNew();
public object AddNew();
abstract member AddNew : unit -> obj
Public Function AddNew () As Object
傳回
新增到收藏中的項目。
範例
以下範例會產生 Window 一個,提示使用者新增一個項目。 接著它呼叫AddNew建立一個新物件,並將 的 DataContext 設Window為該物件。 關於整個範例,請參見 「透過使用 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 都會套用到新項目上。