{"record":{"id":"d630613409cd683b","repo":"dotnet/wpf","slug":"sr-canceleditnotsupported-listcollectionview","errorCode":null,"errorMessage":"SR.CancelEditNotSupported","messagePattern":"SR\\.CancelEditNotSupported","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/ListCollectionView.cs","lineNumber":1299,"sourceCode":"        /// </summary>\n        public void CancelEdit()\n        {\n            if (IsAddingNew)\n                throw new InvalidOperationException(SR.Format(SR.MemberNotAllowedDuringTransaction, \"CancelEdit\", \"AddNew\"));\n            VerifyRefreshNotDeferred();\n\n            if (_editItem == null)\n                return;\n\n            IEditableObject ieo = _editItem as IEditableObject;\n            SetEditItem(null);\n\n            if (ieo != null)\n            {\n                ieo.CancelEdit();\n            }\n            else\n                throw new InvalidOperationException(SR.CancelEditNotSupported);\n        }\n\n        private void ImplicitlyCancelEdit()\n        {\n            IEditableObject ieo = _editItem as IEditableObject;\n            SetEditItem(null);\n\n            ieo?.CancelEdit();\n        }\n\n        /// <summary>\n        /// Returns true if the view supports the notion of \"pending changes\" on the\n        /// current edit item.  This may vary, depending on the view and the particular\n        /// item.  For example, a view might return true if the current edit item\n        /// implements <seealso cref=\"IEditableObject\"/>, or if the view has special\n        /// knowledge about the item that it can use to support rollback of pending\n        /// changes.\n        /// </summary>","sourceCodeStart":1281,"sourceCodeEnd":1317,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/ListCollectionView.cs#L1281-L1317","documentation":"CancelEdit throws InvalidOperationException when the item being edited does not implement IEditableObject. Without IEditableObject, the view has no way to roll back the item's changes, so an explicit CancelEdit is unsupported (pending changes cannot be discarded).","triggerScenarios":"Calling view.CancelEdit() (or ImplicitlyCancelEdit paths like Refresh) while _editItem is a plain object that does not implement System.ComponentModel.IEditableObject.","commonSituations":"Editing POCO/view-model classes that only implement INotifyPropertyChanged but not IEditableObject, then trying to cancel an edit; framework refresh during an open edit transaction on such an item.","solutions":["Make the edited item class implement IEditableObject (BeginEdit/EndEdit/CancelEdit) so changes can be rolled back.","Commit the edit (CommitEdit) instead of cancelling, since cancellation is unsupported for non-IEditableObject items.","Snapshot values manually before EditItem and restore them yourself instead of relying on CancelEdit."],"exampleFix":"// before\nclass Customer : INotifyPropertyChanged { ... }\nview.EditItem(customer);\nview.CancelEdit(); // throws\n\n// after\nclass Customer : INotifyPropertyChanged, IEditableObject\n{\n    public void BeginEdit() { _snapshot = CloneState(); }\n    public void CancelEdit() { RestoreState(_snapshot); }\n    public void EndEdit() { _snapshot = null; }\n}","handlingStrategy":"validation","validationCode":"if (view.IsEditingItem && !(view.CurrentEditItem is IEditableObject))\n    view.CommitEdit(); // cannot CancelEdit non-IEditableObject item\nelse\n    view.CancelEdit();","typeGuard":"bool SupportsCancelEdit(object editItem) => editItem is IEditableObject;","tryCatchPattern":"try { view.CancelEdit(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"CancelEdit\"))\n{ /* fall back: CommitEdit or manually restore values */ }","preventionTips":["Implement IEditableObject on all classes edited through collection views.","Snapshot property values before EditItem if you cannot implement IEditableObject.","Avoid CancelEdit on POCO items; prefer CommitEdit or manual rollback."],"tags":["wpf","collectionview","ieditableobject"],"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"}