{"record":{"id":"b3f3184d10f86b3a","repo":"dotnet/wpf","slug":"throw-new-invalidoperationexception-sr-format-sr-b3f318","errorCode":null,"errorMessage":"throw new InvalidOperationException(SR.Format(SR.MemberNotAllowedForView, \"AddNewItem\"));","messagePattern":"throw new InvalidOperationException\\(SR\\.Format\\(SR\\.MemberNotAllowedForView, \"AddNewItem\"\\)\\);","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Data/CollectionViewProxy.cs","lineNumber":744,"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 = ProxiedView 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        bool ICollectionViewLiveShaping.CanChangeLiveSorting\n        {\n            get\n            {\n                ICollectionViewLiveShaping cvls = ProxiedView as ICollectionViewLiveShaping;\n                return (cvls != null) ? cvls.CanChangeLiveSorting : false;\n            }\n        }","sourceCodeStart":726,"sourceCodeEnd":762,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Data/CollectionViewProxy.cs#L726-L762","documentation":"CollectionViewProxy.AddNewItem(newItem) is the IEditableCollectionViewAddNewItem implementation; it forwards to the proxied view's IEditableCollectionViewAddNewItem. If the wrapped view does not implement that interface (null `ani` at CollectionViewProxy.cs:744), the proxy throws InvalidOperationException: adding a typed new item is not allowed for this view.","triggerScenarios":"Calling AddNewItem on a CollectionViewProxy whose ProxiedView implements IEditableCollectionView but not IEditableCollectionViewAddNewItem, or neither.","commonSituations":"Programmatic typed adds (distinct from AddNew) on views lacking the AddNewItem contract; custom collection views that only implemented part of the editable-collection surface.","solutions":["Check whether ProxiedView as IEditableCollectionViewAddNewItem is non-null (or use CanAddNewItem) before calling","Fall back to AddNew() plus assigning properties when IEditableCollectionViewAddNewItem is unavailable","Implement IEditableCollectionViewAddNewItem on the proxied view if typed adds are required"],"exampleFix":"// before\nproxy.AddNewItem(newItem);\n// after\nif (proxy.CanAddNewItem)\n{\n    proxy.AddNewItem(newItem);\n}\nelse if (proxy.CanAddNew)\n{\n    proxy.AddNew();\n}","handlingStrategy":"validation","validationCode":"if (!(proxy.ProxiedView is IEditableCollectionViewAddNewItem)) return;\nproxy.AddNewItem(newItem);","typeGuard":"bool SupportsAddNewItem(CollectionViewProxy p) => p?.ProxiedView is IEditableCollectionViewAddNewItem;","tryCatchPattern":"try { proxy.AddNewItem(newItem); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"AddNewItem\")) { /* fall back to AddNew + property assignment */ }","preventionTips":["Distinguish IEditableCollectionViewAddNewItem from IEditableCollectionView capability checks","Use AddNew() when typed AddNewItem is unavailable","Implement IEditableCollectionViewAddNewItem on custom views that need typed adds"],"tags":["wpf","collection-view","invalidoperation","add-item"],"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"}