{"record":{"id":"de1d7be5ee4be20a","repo":"dotnet/wpf","slug":"sr-format-sr-membernotallowedforview-addnewitem","errorCode":null,"errorMessage":"SR.Format(SR.MemberNotAllowedForView, \"AddNewItem\")","messagePattern":"SR\\.Format\\(SR\\.MemberNotAllowedForView, \"AddNewItem\"\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ItemCollection.cs","lineNumber":1224,"sourceCode":"            }\n        }\n\n        /// <summary>\n        /// Add a new item to the underlying collection.  Returns the new item.\n        /// After calling AddNewItem and changing the new item as desired, either\n        /// <seealso cref=\"IEditableCollectionView.CommitNew\"/> or <seealso cref=\"IEditableCollectionView.CancelNew\"/> should be\n        /// called to complete the transaction.\n        /// </summary>\n        object  IEditableCollectionViewAddNewItem.AddNewItem(object newItem)\n        {\n            IEditableCollectionViewAddNewItem ani = _collectionView as IEditableCollectionViewAddNewItem;\n            if (ani != null)\n            {\n                return ani.AddNewItem(newItem);\n            }\n            else\n            {\n                throw new InvalidOperationException(SR.Format(SR.MemberNotAllowedForView, \"AddNewItem\"));\n            }\n        }\n\n        #endregion IEditableCollectionViewAddNewItem\n\n        #region ICollectionViewLiveShaping\n\n        ///<summary>\n        /// Gets a value that indicates whether this view supports turning live sorting on or off.\n        ///</summary>\n        public bool CanChangeLiveSorting\n        {\n            get\n            {\n                ICollectionViewLiveShaping cvls = _collectionView as ICollectionViewLiveShaping;\n                return (cvls != null) ? cvls.CanChangeLiveSorting : false;\n            }\n        }","sourceCodeStart":1206,"sourceCodeEnd":1242,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ItemCollection.cs#L1206-L1242","documentation":"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\").","triggerScenarios":"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.","commonSituations":"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.","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)"],"exampleFix":"// before\nitems.AddNewItem(newItem);\n// after\nif (items.CanAddNewItem) { items.AddNewItem(newItem); } else { items.Add(newItem); }","handlingStrategy":"validation","validationCode":"if (items.CanAddNewItem) items.AddNewItem(newItem); else items.Add(newItem);","typeGuard":"bool canAddNew = items.CollectionView is IEditableCollectionViewAddNewItem ani && ani.CanAddNewItem;","tryCatchPattern":"try { items.AddNewItem(newItem); } catch (InvalidOperationException ex) { items.Add(newItem); }","preventionTips":["Check CanAddNewItem first","Prefer plain Add() unless the view is IEditableCollectionViewAddNewItem","Don't confuse AddNewItem with Add"],"tags":["wpf","itemcollection","addnewitem"],"backgroundTag":"unsupported-operation","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}