{"record":{"id":"378d3cc5f81c9f72","repo":"dotnet/wpf","slug":"sr-format-sr-membernotallowedforview-addnew-378d3c","errorCode":null,"errorMessage":"SR.Format(SR.MemberNotAllowedForView, \"AddNew\")","messagePattern":"SR\\.Format\\(SR\\.MemberNotAllowedForView, \"AddNew\"\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/ListCollectionView.cs","lineNumber":716,"sourceCode":"        /// <summary>\n        /// Add a new item to the underlying collection.  Returns the new item.\n        /// After calling AddNew 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 AddNew()\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 (!CanAddNew)\n                throw new InvalidOperationException(SR.Format(SR.MemberNotAllowedForView, \"AddNew\"));\n\n            return AddNewCommon(_itemConstructor.Invoke(null));\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=\"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            }","sourceCodeStart":698,"sourceCodeEnd":734,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/ListCollectionView.cs#L698-L734","documentation":"ListCollectionView.AddNew is only legal when CanAddNew is true — i.e. the underlying collection supports adding (IBindingList/AddNew semantics or the collection is not read-only/fixed-size) and any AddNew transaction limit is respected. Otherwise it throws InvalidOperationException with MemberNotAllowedForView(\"AddNew\").","triggerScenarios":"Calling AddNew() when the underlying IList is read-only or fixed-size, when a NewItemFactory/constructor is unavailable for the item type, or when the collection does not permit new items (CanAddNew false).","commonSituations":"DataGrid 'add row' feature bound to a read-only collection (e.g. a computed/frozen list, an array, or a list exposed without setters); MVVM code calling AddNew on views over immutable data.","solutions":["Check CanAddNew before calling AddNew and disable the Add UI when it is false","Make the underlying collection writable: use a List<T>/ObservableCollection<T> instead of a read-only or fixed-size list","Provide a public parameterless constructor (or NewItemFactory) for the item type so the view can create instances"],"exampleFix":"// before\nvar newItem = (MyItem)view.AddNew(); // throws on read-only source\n// after\nif (((IEditableCollectionView)view).CanAddNew)\n{\n    var newItem = (MyItem)view.AddNew();\n}\nelse { /* disable add UI */ }","handlingStrategy":"validation","validationCode":"var ecv = (IEditableCollectionView)view;\nif (ecv.CanAddNew)\n{\n    var item = ecv.AddNew();\n}\nelse { /* disable add feature or use a writable collection */ }","typeGuard":null,"tryCatchPattern":"try { var item = view.AddNew(); }\ncatch (InvalidOperationException)\n{\n    // underlying collection does not support adding; surface to user\n}","preventionTips":["Check CanAddNew before exposing Add UI in DataGrid or MVVM commands","Back views with ObservableCollection<T>/List<T>, not arrays or read-only lists","Ensure item types have public parameterless constructors when relying on AddNew"],"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"}