{"record":{"id":"5b834b7d24f1f5f2","repo":"dotnet/wpf","slug":"sr-format-sr-membernotallowedforview-addnewitem-5b834b","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/Data/ListCollectionView.cs","lineNumber":739,"sourceCode":"        /// <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=\"CommitNew\"/> or <seealso cref=\"CancelNew\"/> should be\n        /// called to complete the transaction.\n        /// </summary>\n        public object AddNewItem(object newItem)\n        {\n            VerifyRefreshNotDeferred();\n\n            if (IsEditingItem)\n            {\n                CommitEdit();   // implicitly close a previous EditItem\n            }\n\n            CommitNew();        // implicitly close a previous AddNew\n\n            if (!CanAddNewItem)\n                throw new InvalidOperationException(SR.Format(SR.MemberNotAllowedForView, \"AddNewItem\"));\n\n            return AddNewCommon(newItem);\n        }\n\n        private object AddNewCommon(object newItem)\n        {\n            BindingOperations.AccessCollection(SourceList,\n                () =>\n                {\n                    ProcessPendingChanges();    // bring the shadow list up to date\n\n                    _newItemIndex = -2; // this is a signal that the next Add event comes from AddNew\n                    int index = SourceList.Add(newItem);\n\n                    // if the source doesn't raise collection change events, fake one\n                    if (!(SourceList is INotifyCollectionChanged))\n                    {\n                        // the index returned by IList.Add isn't always reliable","sourceCodeStart":721,"sourceCodeEnd":757,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/ListCollectionView.cs#L721-L757","documentation":"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\").","triggerScenarios":"Calling AddNewItem(item) when the underlying IList is read-only or fixed-size, or otherwise cannot contain the given item (CanAddNewItem false).","commonSituations":"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.","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"],"exampleFix":"// before\nview.AddNewItem(myItem); // throws on read-only source\n// after\nif (((IEditableCollectionView)view).CanAddNewItem)\n    view.AddNewItem(myItem);\nelse\n    throw new InvalidOperationException(\"Source collection is read-only\");","handlingStrategy":"validation","validationCode":"var ecv = (IEditableCollectionView)view;\nif (ecv.CanAddNewItem)\n{\n    ecv.AddNewItem(myItem);\n}\nelse { /* use a writable source collection */ }","typeGuard":null,"tryCatchPattern":"try { view.AddNewItem(item); }\ncatch (InvalidOperationException)\n{\n    // cannot add the given item to the source collection\n}","preventionTips":["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"],"tags":["wpf","data-binding","invalidoperationexception","listcollectionview","read-only"],"backgroundTag":"operation-not-supported","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}