{"record":{"id":"91847b5d633161b5","repo":"dotnet/wpf","slug":"sr-format-sr-membernotallowedforview-cancelnew","errorCode":null,"errorMessage":"SR.Format(SR.MemberNotAllowedForView, \"CancelNew\")","messagePattern":"SR\\.Format\\(SR\\.MemberNotAllowedForView, \"CancelNew\"\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ItemCollection.cs","lineNumber":962,"sourceCode":"            {\n                throw new InvalidOperationException(SR.Format(SR.MemberNotAllowedForView, \"CommitNew\"));\n            }\n        }\n\n        /// <summary>\n        /// Complete the transaction started by <seealso cref=\"IEditableCollectionView.AddNew\"/>.  The new\n        /// item is removed from the collection.\n        /// </summary>\n        void    IEditableCollectionView.CancelNew()\n        {\n            IEditableCollectionView ecv = _collectionView as IEditableCollectionView;\n            if (ecv != null)\n            {\n                ecv.CancelNew();\n            }\n            else\n            {\n                throw new InvalidOperationException(SR.Format(SR.MemberNotAllowedForView, \"CancelNew\"));\n            }\n        }\n\n        /// <summary>\n        /// Returns true if an </seealso cref=\"IEditableCollectionView.AddNew\"> transaction is in progress.\n        /// </summary>\n        bool    IEditableCollectionView.IsAddingNew\n        {\n            get\n            {\n                IEditableCollectionView ecv = _collectionView as IEditableCollectionView;\n                if (ecv != null)\n                {\n                    return ecv.IsAddingNew;\n                }\n                else\n                {\n                    return false;","sourceCodeStart":944,"sourceCodeEnd":980,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ItemCollection.cs#L944-L980","documentation":"ItemCollection.CancelNew throws InvalidOperationException(SR.MemberNotAllowedForView, \"CancelNew\") when the underlying view does not implement IEditableCollectionView, so there is no AddNew transaction to roll back. The wrapper only supports this on editing-capable views.","triggerScenarios":"Calling items.CancelNew() when ItemsSource's view is not an IEditableCollectionView.","commonSituations":"Cancel button handlers in add-item dialogs bound to sources without editable views; switching ItemsSource while an add transaction is open.","solutions":["Use a collection source whose view implements IEditableCollectionView.","Guard with `items.CanCancelNew` before calling CancelNew.","Track add transactions yourself (dispose/rollback logic) if the view cannot be changed."],"exampleFix":"// before\nitems.CancelNew();\n// after\nif (items.CanCancelNew)\n    items.CancelNew();","handlingStrategy":"validation","validationCode":"if (items.CanCancelNew)\n    items.CancelNew();","typeGuard":"static bool CanCancel(ItemCollection c) => c.CollectionView is IEditableCollectionView;","tryCatchPattern":"try { items.CancelNew(); }\ncatch (InvalidOperationException) { /* view not editable; discard new item manually */ }","preventionTips":["Check CanCancelNew before CancelNew","Keep the same editable view attached across the transaction","Handle cancel in UI only when the view supports editing"],"tags":["wpf","itemcollection","invalidoperationexception","cancelnew"],"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"}