{"record":{"id":"7b8567765ac975ae","repo":"dotnet/wpf","slug":"sr-format-sr-membernotallowedforview-addnew","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/Controls/ItemCollection.cs","lineNumber":926,"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 = _collectionView 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 = _collectionView as IEditableCollectionView;\n            if (ecv != null)\n            {\n                ecv.CommitNew();\n            }\n            else\n            {","sourceCodeStart":908,"sourceCodeEnd":944,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ItemCollection.cs#L908-L944","documentation":"ItemCollection.AddNew throws InvalidOperationException(SR.MemberNotAllowedForView, \"AddNew\") when the underlying collection view does not implement IEditableCollectionView, so the AddNew transactional-add feature is unavailable. The ItemCollection only forwards the call when the view supports editing.","triggerScenarios":"Calling items.AddNew() when ItemsSource's view is not an IEditableCollectionView.","commonSituations":"Using AddNew with a read-only or plain collection source; switching ItemsSource at runtime to a type whose view lacks editing support while edit-related code still runs.","solutions":["Ensure ItemsSource is bound to a collection whose default view implements IEditableCollectionView (e.g. ObservableCollection<T>).","Feature-test first: `if (items.CanAddNew) items.AddNew();`.","Fall back to Items.Add for non-editable views when no transactional add is needed."],"exampleFix":"// before\nvar newItem = items.AddNew();\n// after\nif (!items.CanAddNew)\n    throw new InvalidOperationException(\"View does not support AddNew\");\nvar newItem = items.AddNew();","handlingStrategy":"validation","validationCode":"if (items.CanAddNew)\n    var obj = items.AddNew();\nelse\n    var obj = items.AddNewFallback(); // e.g. items.Add(new T())","typeGuard":"static bool CanAdd(ItemCollection c) => c.CollectionView is IEditableCollectionView && c.CanAddNew;","tryCatchPattern":"try { return items.AddNew(); }\ncatch (InvalidOperationException) { var o = new T(); items.Add(o); return o; }","preventionTips":["Check CanAddNew before AddNew","Use ObservableCollection<T> as ItemsSource for editing scenarios","Keep edit-related UI (Add/Commit/Cancel) hidden when the view lacks editing support"],"tags":["wpf","itemcollection","invalidoperationexception","addnew"],"backgroundTag":"unsupported-operation","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"}