{"record":{"id":"255aee923363c1a3","repo":"dotnet/wpf","slug":"throw-new-invalidoperationexception-sr-format-sr-255aee","errorCode":null,"errorMessage":"throw new InvalidOperationException(SR.Format(SR.MemberNotAllowedForView, \"AddNew\"));","messagePattern":"throw new InvalidOperationException\\(SR\\.Format\\(SR\\.MemberNotAllowedForView, \"AddNew\"\\)\\);","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Data/CollectionViewProxy.cs","lineNumber":446,"sourceCode":"            }\n        }\n\n        /// <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=\"IEditableCollectionView.CommitNew\"/> or <seealso cref=\"IEditableCollectionView.CancelNew\"/> should be\n        /// called to complete the transaction.\n        /// </summary>\n        object IEditableCollectionView.AddNew()\n        {\n            IEditableCollectionView ecv = ProxiedView as IEditableCollectionView;\n            if (ecv != null)\n            {\n                return ecv.AddNew();\n            }\n            else\n            {\n                throw new InvalidOperationException(SR.Format(SR.MemberNotAllowedForView, \"AddNew\"));\n            }\n        }\n\n\n        /// <summary>\n        /// Complete the transaction started by <seealso cref=\"IEditableCollectionView.AddNew\"/>.  The new\n        /// item remains in the collection, and the view's sort, filter, and grouping\n        /// specifications (if any) are applied to the new item.\n        /// </summary>\n        void IEditableCollectionView.CommitNew()\n        {\n            IEditableCollectionView ecv = ProxiedView as IEditableCollectionView;\n            if (ecv != null)\n            {\n                ecv.CommitNew();\n            }\n            else\n            {","sourceCodeStart":428,"sourceCodeEnd":464,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Data/CollectionViewProxy.cs#L428-L464","documentation":"CollectionViewProxy.AddNew throws InvalidOperationException(MemberNotAllowedForView, \"AddNew\") when the underlying view does not implement IEditableCollectionView, meaning the proxied collection cannot support the AddNew transaction.","triggerScenarios":"Calling IEditableCollectionView.AddNew via a CollectionViewProxy wrapping a non-editable view (view over a non-IList source, or a read-only collection).","commonSituations":"Toolbar 'Add new item' commands bound to views over read-only or non-editable sources; CanAddNew not checked before invoking AddNew; swapping data sources so the proxy no longer wraps an editable view.","solutions":["Guard with view.CanAddNew (or an IEditableCollectionView check) before calling AddNew","Bind UI to an editable collection (e.g. ObservableCollection or List-backed view)","Disable add-new UI when the current view lacks editing support"],"exampleFix":"// before\nvar newItem = ((IEditableCollectionView)view).AddNew();\n// after\nvar ecv = view as IEditableCollectionView;\nif (ecv != null && ecv.CanAddNew)\n    var newItem = ecv.AddNew();","handlingStrategy":"type-guard","validationCode":"if (view is IEditableCollectionView ecv && ecv.CanAddNew) { var item = ecv.AddNew(); }","typeGuard":"bool CanAddNewItem(ICollectionView v) => v is IEditableCollectionView ecv && ecv.CanAddNew;","tryCatchPattern":"try { proxy.AddNew(); } catch (InvalidOperationException) { /* view not editable; disable add-new command */ }","preventionTips":["Check CanAddNew before invoking AddNew","Use ObservableCollection or List-backed sources for editable views","Update command CanExecute when the underlying view changes"],"tags":["wpf","collectionview","unsupported-operation"],"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"}