{"record":{"id":"11c1b3a5f3474961","repo":"dotnet/wpf","slug":"sr-canceleditnotsupported","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/BindingListCollectionView.cs","lineNumber":1033,"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":1015,"sourceCodeEnd":1051,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/BindingListCollectionView.cs#L1015-L1051","documentation":"CancelEdit() supports true cancellation only when the edited item implements IEditableObject, in which case IEditableObject.CancelEdit() is invoked. If the item does not implement IEditableObject (and IEditableCollectionView mode is not 'simple'), the view throws InvalidOperationException because it cannot restore the item's prior state.","triggerScenarios":"Calling CancelEdit() (or triggering it via DataGrid escape/UI) while the current edit item is a plain POCO that does not implement IEditableObject.","commonSituations":"Binding to a list of simple model classes without IEditableObject and pressing Escape in a DataGrid edit; expecting automatic rollback of property changes on objects that only implement INotifyPropertyChanged.","solutions":["Implement IEditableObject on the item class (BeginEdit/CancelEdit/EndEdit) so the view can roll back changes.","Wrap items in an editable view-model that snapshots state in BeginEdit and restores it in CancelEdit.","Avoid CancelEdit for such items: snapshot values yourself before EditItem and restore manually.","Check (view as IEditableCollectionView).CanCancelEdit before calling CancelEdit and handle the false case."],"exampleFix":"// before\npublic class Customer {\n    public string Name { get; set; }\n}\n\n// after\npublic class Customer : IEditableObject {\n    public string Name { get; set; }\n    private string _backup;\n    public void BeginEdit() => _backup = Name;\n    public void CancelEdit() => Name = _backup;\n    public void EndEdit() => _backup = null;\n}","handlingStrategy":"validation","validationCode":"// C#\nvar iecv = view as IEditableCollectionView;\nif (iecv != null && !iecv.CanCancelEdit) { /* snapshot & restore manually instead */ }","typeGuard":"bool SupportsCancelEdit(IEditableCollectionView v) => v.CanCancelEdit;","tryCatchPattern":"try { view.CancelEdit(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"CancelEdit\")) { /* implement IEditableObject or restore manually */ }","preventionTips":["Implement IEditableObject on all editable item types.","Check IEditableCollectionView.CanCancelEdit before CancelEdit.","Keep a manual snapshot in your view-model for items that can't implement IEditableObject."],"tags":["wpf","data-binding","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-21T21:30:21.729Z"}