dotnet/wpf · error · InvalidOperationException

SR.CurrentChangingCannotBeCanceled

Error message

SR.CurrentChangingCannotBeCanceled

What it means

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.

Solutions

  1. Check e.IsCancelable before assigning e.Cancel = true
  2. Skip cancellation logic when IsCancelable is false and record the pending change for after the operation
  3. Use the cancelable constructor new CurrentChangingEventArgs(true) when you control event raising and need cancellation

Example fix

// before
void OnCurrentChanging(object s, CurrentChangingEventArgs e) { e.Cancel = true; }
// after
void OnCurrentChanging(object s, CurrentChangingEventArgs e) { if (e.IsCancelable) e.Cancel = !CanMoveCurrent(); }
Defensive patterns

Strategy: validation

Validate before calling

if (e.IsCancelable) e.Cancel = shouldCancel;

Type guard

static bool CanCancel(CurrentChangingEventArgs e) => e.IsCancelable;

Try / catch

try { e.Cancel = true; } catch (InvalidOperationException) { /* non-cancelable change; defer or ignore */ }

Prevention

When it happens

Trigger: 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).

Common situations: Handling CollectionView/ItemCollection CurrentChanging in WPF and cancelling navigation without checking IsCancelable; handlers shared between cancelable and non-cancelable change events.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/6a81ce690c5304f2. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/System/ComponentModel/CurrentChangingEventArgs.cs:77

        /// <summary>
        /// When set to true, this event will be canceled.
        /// </summary>
        /// <remarks>
        /// If IsCancelable is False, setting this value to True will cause an InvalidOperationException to be thrown.
        /// </remarks>
        public bool Cancel
        {
            get { return _cancel; }
            set
            {
                if (IsCancelable)
                {
                    _cancel = value;
                }
                else if (value)
                {
                    throw new InvalidOperationException(SR.CurrentChangingCannotBeCanceled);
                }
            }
        }

        //------------------------------------------------------
        //
        //  Private Fields
        //
        //------------------------------------------------------

        private bool _cancel = false;
        private bool _isCancelable;
    }

    /// <summary>
    ///     The delegate to use for handlers that receive the CurrentChanging event.
    /// </summary>
    public delegate void CurrentChangingEventHandler(object sender, CurrentChangingEventArgs e);

View on GitHub (pinned to 81131a70a4)