dotnet/wpf · error · InvalidOperationException
SR.Format(SR.MemberNotAllowedForView, "AddNewItem")
Error message
SR.Format(SR.MemberNotAllowedForView, "AddNewItem")
What it means
ListCollectionView.AddNewItem(object) requires CanAddNewItem — the underlying collection must accept the supplied item (writable, correct type). When it cannot, the view throws InvalidOperationException with MemberNotAllowedForView("AddNewItem").
Solutions
- Check CanAddNewItem before calling AddNewItem and disable the add path otherwise
- Back the view with a writable collection (ObservableCollection<T> or List<T>)
- Ensure the item's type is compatible with the underlying collection's element type
Example fix
// before
view.AddNewItem(myItem); // throws on read-only source
// after
if (((IEditableCollectionView)view).CanAddNewItem)
view.AddNewItem(myItem);
else
throw new InvalidOperationException("Source collection is read-only"); Defensive patterns
Strategy: validation
Validate before calling
var ecv = (IEditableCollectionView)view;
if (ecv.CanAddNewItem)
{
ecv.AddNewItem(myItem);
}
else { /* use a writable source collection */ } Try / catch
try { view.AddNewItem(item); }
catch (InvalidOperationException)
{
// cannot add the given item to the source collection
} Prevention
- Check CanAddNewItem before calling AddNewItem
- Use writable, type-compatible collections as binding sources
- Verify the item's runtime type matches the underlying collection's element type
When it happens
Trigger: Calling AddNewItem(item) when the underlying IList is read-only or fixed-size, or otherwise cannot contain the given item (CanAddNewItem false).
Common situations: Adding pre-constructed objects to a view over a read-only source; binding to arrays or immutable collections and then invoking AddNewItem from UI commands.
Related errors
- SR.Format(SR.MemberNotAllowedForView, "AddNew")
- SR.Format(SR.MemberNotAllowedDuringAddOrEdit, "CustomSort")
- SR.Format(SR.MemberNotAllowedDuringAddOrEdit, "Filter")
- SR.Format(SR.MemberNotAllowedDuringAddOrEdit, "Grouping")
- SR.Format(SR.MemberNotAllowedDuringTransaction…
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/5b834b7d24f1f5f2.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/ListCollectionView.cs:739
/// <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="CommitNew"/> or <seealso cref="CancelNew"/> should be
/// called to complete the transaction.
/// </summary>
public object AddNewItem(object newItem)
{
VerifyRefreshNotDeferred();
if (IsEditingItem)
{
CommitEdit(); // implicitly close a previous EditItem
}
CommitNew(); // implicitly close a previous AddNew
if (!CanAddNewItem)
throw new InvalidOperationException(SR.Format(SR.MemberNotAllowedForView, "AddNewItem"));
return AddNewCommon(newItem);
}
private object AddNewCommon(object newItem)
{
BindingOperations.AccessCollection(SourceList,
() =>
{
ProcessPendingChanges(); // bring the shadow list up to date
_newItemIndex = -2; // this is a signal that the next Add event comes from AddNew
int index = SourceList.Add(newItem);
// if the source doesn't raise collection change events, fake one
if (!(SourceList is INotifyCollectionChanged))
{
// the index returned by IList.Add isn't always reliableView on GitHub (pinned to 81131a70a4)