IEditableCollectionView.CanAddNew Tulajdonság
Definíció
Fontos
Egyes információk olyan, kiadás előtti termékekre vonatkoznak, amelyek a kiadásig még jelentősen módosulhatnak. A Microsoft nem vállal kifejezett vagy törvényi garanciát az itt megjelenő információért.
Beolvas egy értéket, amely jelzi, hogy hozzáadható-e új elem a gyűjteményhez.
public:
property bool CanAddNew { bool get(); };
public bool CanAddNew { get; }
member this.CanAddNew : bool
Public ReadOnly Property CanAddNew As Boolean
Tulajdonság értéke
trueha egy új elem hozzáadható a gyűjteményhez; egyéb esetben. false
Példák
Az alábbi példa azt ellenőrzi, hogy hozzáadható-e egy elem a gyűjteményhez. Ha CanAddNew igen false, a példa azt jelzi a felhasználónak, hogy nem adható hozzá elem. Ellenkező esetben megjelenik egy űrlap, amely arra kéri a felhasználót, hogy adjon hozzá egy új elemet. A teljes minta esetében lásd: Gyűjtemények módosítása az IEditableCollectionView minta használatával.
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
Megjegyzések
Ha IEditableCollectionView az alábbiak teljesülnek, új elemet adhat hozzá:
Egy elem hozzáadható az alapul szolgáló gyűjteményhez. Ha például a gyűjtemény írásvédett, CanAddNew akkor az .
falseAz IEditableCollectionView objektum a gyűjteményben található típusú objektumot hozhat létre. Ha például a gyűjtemény típusa ObservableCollection<T>, akkor létre IEditableCollectionView kell tudnia hozni egy típusú
Tobjektumot.