IEditableCollectionView.CanAddNew プロパティ

定義

新しい項目をコレクションに追加できるかどうかを示す値を取得します。

public:
 property bool CanAddNew { bool get(); };
public bool CanAddNew { get; }
member this.CanAddNew : bool
Public ReadOnly Property CanAddNew As Boolean

プロパティ値

true 新しい項目をコレクションに追加できる場合。それ以外の場合は false

次の例では、項目をコレクションに追加できるかどうかを確認します。 CanAddNewfalse場合、この例では項目を追加できないことをユーザーに通知します。 それ以外の場合は、ユーザーに新しい項目の追加を求めるフォームが表示されます。 サンプル全体については、「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

注釈

次の条件に該当する場合、 IEditableCollectionView は新しい項目を追加できます。

  • 基になるコレクションに項目を追加できます。 たとえば、コレクションが読み取り専用の場合、 CanAddNewfalse

  • IEditableCollectionViewは、コレクション内にある型のオブジェクトを作成できます。 たとえば、コレクションが ObservableCollection<T>型の場合、 IEditableCollectionViewT型のオブジェクトを作成できる必要があります。

適用対象