{"record":{"id":"6a81ce690c5304f2","repo":"dotnet/wpf","slug":"sr-currentchangingcannotbecanceled","errorCode":null,"errorMessage":"SR.CurrentChangingCannotBeCanceled","messagePattern":"SR\\.CurrentChangingCannotBeCanceled","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/ComponentModel/CurrentChangingEventArgs.cs","lineNumber":77,"sourceCode":"\n        /// <summary>\n        /// When set to true, this event will be canceled.\n        /// </summary>\n        /// <remarks>\n        /// If IsCancelable is False, setting this value to True will cause an InvalidOperationException to be thrown.\n        /// </remarks>\n        public bool Cancel\n        {\n            get { return _cancel; }\n            set\n            {\n                if (IsCancelable)\n                {\n                    _cancel = value;\n                }\n                else if (value)\n                {\n                    throw new InvalidOperationException(SR.CurrentChangingCannotBeCanceled);\n                }\n            }\n        }\n\n        //------------------------------------------------------\n        //\n        //  Private Fields\n        //\n        //------------------------------------------------------\n\n        private bool _cancel = false;\n        private bool _isCancelable;\n    }\n\n    /// <summary>\n    ///     The delegate to use for handlers that receive the CurrentChanging event.\n    /// </summary>\n    public delegate void CurrentChangingEventHandler(object sender, CurrentChangingEventArgs e);","sourceCodeStart":59,"sourceCodeEnd":95,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/ComponentModel/CurrentChangingEventArgs.cs#L59-L95","documentation":"CurrentChangingEventArgs.Cancel can only be set to true when the event args were constructed as cancelable (IsCancelable == true). Setting Cancel=true on non-cancelable args throws InvalidOperationException with SR.CurrentChangingCannotBeCanceled. Non-cancelable args are used by collection implementations that raise CurrentChanging as a notification only.","triggerScenarios":"In a CurrentChanging handler, setting e.Cancel = true when e.IsCancelable is false (e.g. ICollectionView.CurrentChanging raised with cancelable=false, or the handler runs for a non-cancelable bulk change).","commonSituations":"Handling CollectionView/ItemCollection CurrentChanging in WPF and cancelling navigation without checking IsCancelable; handlers shared between cancelable and non-cancelable change events.","solutions":["Check e.IsCancelable before assigning e.Cancel = true","Skip cancellation logic when IsCancelable is false and record the pending change for after the operation","Use the cancelable constructor new CurrentChangingEventArgs(true) when you control event raising and need cancellation"],"exampleFix":"// before\nvoid OnCurrentChanging(object s, CurrentChangingEventArgs e) { e.Cancel = true; }\n// after\nvoid OnCurrentChanging(object s, CurrentChangingEventArgs e) { if (e.IsCancelable) e.Cancel = !CanMoveCurrent(); }","handlingStrategy":"validation","validationCode":"if (e.IsCancelable) e.Cancel = shouldCancel;","typeGuard":"static bool CanCancel(CurrentChangingEventArgs e) => e.IsCancelable;","tryCatchPattern":"try { e.Cancel = true; } catch (InvalidOperationException) { /* non-cancelable change; defer or ignore */ }","preventionTips":["Always test IsCancelable before setting Cancel","Share handlers only via a wrapper that checks IsCancelable","When raising CurrentChanging yourself, pass cancelable:true only if cancellation is honored"],"tags":["wpf","collectionview","invalid-operation","event-args"],"backgroundTag":"invalid-state-transition","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"}