dotnet/wpf · error · InvalidOperationException
SR.Format(SR.MemberNotAllowedForView, "AddNewItem")
Error message
SR.Format(SR.MemberNotAllowedForView, "AddNewItem")
What it means
ItemCollection.AddNewItem adds a new item via the IEditableCollectionViewAddNewItem interface; it only works when the active view implements that interface. Otherwise it throws InvalidOperationException with MemberNotAllowedForView("AddNewItem").
Solutions
- Check CanAddNewItem before calling AddNewItem
- Use ItemCollection.Add() for simple insertion into an internal ItemsCollection instead
- Bind to a source whose default view supports AddNewItem (e.g. IBindingList / ObservableCollection with an editable view)
Example fix
// before
items.AddNewItem(newItem);
// after
if (items.CanAddNewItem) { items.AddNewItem(newItem); } else { items.Add(newItem); } Defensive patterns
Strategy: validation
Validate before calling
if (items.CanAddNewItem) items.AddNewItem(newItem); else items.Add(newItem);
Type guard
bool canAddNew = items.CollectionView is IEditableCollectionViewAddNewItem ani && ani.CanAddNewItem;
Try / catch
try { items.AddNewItem(newItem); } catch (InvalidOperationException ex) { items.Add(newItem); } Prevention
- Check CanAddNewItem first
- Prefer plain Add() unless the view is IEditableCollectionViewAddNewItem
- Don't confuse AddNewItem with Add
When it happens
Trigger: Calling ItemCollection.AddNewItem(newItem) when the underlying view does not implement IEditableCollectionViewAddNewItem (CanAddNewItem is false), e.g. a plain read-only or non-add-capable source view.
Common situations: Adding items programmatically to a DataGrid/ItemsControl bound to sources like LINQ results or custom views that don't support AddNew; confusing AddNewItem with the plain Add() method.
Understand the failure class
Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.
Related errors
- SR.BadTargetArray
- SR.CannotUseItemsSource
- SR.Format(SR.MemberNotAllowedForView…
- SR.Format(SR.MemberNotAllowedForView, "AddNew")
- SR.Format(SR.MemberNotAllowedForView, "CancelEdit")
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/de1d7be5ee4be20a.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ItemCollection.cs:1224
}
}
/// <summary>
/// Add a new item to the underlying collection. Returns the new item.
/// After calling AddNewItem and changing the new item as desired, either
/// <seealso cref="IEditableCollectionView.CommitNew"/> or <seealso cref="IEditableCollectionView.CancelNew"/> should be
/// called to complete the transaction.
/// </summary>
object IEditableCollectionViewAddNewItem.AddNewItem(object newItem)
{
IEditableCollectionViewAddNewItem ani = _collectionView as IEditableCollectionViewAddNewItem;
if (ani != null)
{
return ani.AddNewItem(newItem);
}
else
{
throw new InvalidOperationException(SR.Format(SR.MemberNotAllowedForView, "AddNewItem"));
}
}
#endregion IEditableCollectionViewAddNewItem
#region ICollectionViewLiveShaping
///<summary>
/// Gets a value that indicates whether this view supports turning live sorting on or off.
///</summary>
public bool CanChangeLiveSorting
{
get
{
ICollectionViewLiveShaping cvls = _collectionView as ICollectionViewLiveShaping;
return (cvls != null) ? cvls.CanChangeLiveSorting : false;
}
}View on GitHub (pinned to 81131a70a4)