dotnet/wpf · error · NotSupportedException

SR.Format(SR.UnexpectedCollectionChangeAction, e.Action)

Error message

SR.Format(SR.UnexpectedCollectionChangeAction, e.Action)

What it means

The default branch of ValidateCollectionChangedEventArgs throws NotSupportedException for any NotifyCollectionChangedAction value it does not recognize, formatted with UnexpectedCollectionChangeAction. This is a defensive catch-all against corrupt or future/invalid action enums reaching the view.

Solutions

  1. Fix the producer of the event to raise only valid NotifyCollectionChangedAction values (Add/Remove/Replace/Move/Reset).
  2. Validate/cast enum values defensively before constructing NotifyCollectionChangedEventArgs.
  3. Update the WPF runtime if a newer NotifyCollectionChangedAction member is the source (unlikely; enum is closed).

Example fix

// before
var args = new NotifyCollectionChangedEventArgs((NotifyCollectionChangedAction)99, item); // throws

// after
var args = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item);
Defensive patterns

Strategy: validation

Validate before calling

if (!Enum.IsDefined(typeof(NotifyCollectionChangedAction), action))
    throw new ArgumentOutOfRangeException(nameof(action));

Type guard

bool IsValidAction(NotifyCollectionChangedAction a) =>
    a is NotifyCollectionChangedAction.Add or Remove or Replace or Move or Reset;

Try / catch

try { ApplyChange(e); } catch (NotSupportedException) { view.Refresh(); }

Prevention

When it happens

Trigger: A source collection raises a CollectionChanged event with an out-of-range or undefined NotifyCollectionChangedAction (e.g. cast from an invalid int, or a hand-constructed event args with a bogus action).

Common situations: Custom INotifyCollectionChanged implementations building their own NotifyCollectionChangedEventArgs with an invalid action enum value; unsafe casts of external event data into NotifyCollectionChangedAction; P/Invoke or serialization paths that fabricate change events.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/BindingListCollectionView.cs:2457

                    break;

                case NotifyCollectionChangedAction.Replace:
                    if (e.NewItems.Count != 1 || e.OldItems.Count != 1)
                        throw new NotSupportedException(SR.RangeActionsNotSupported);
                    break;

                case NotifyCollectionChangedAction.Move:
                    if (e.NewItems.Count != 1)
                        throw new NotSupportedException(SR.RangeActionsNotSupported);
                    if (e.NewStartingIndex < 0)
                        throw new InvalidOperationException(SR.CannotMoveToUnknownPosition);
                    break;

                case NotifyCollectionChangedAction.Reset:
                    break;

                default:
                    throw new NotSupportedException(SR.Format(SR.UnexpectedCollectionChangeAction, e.Action));
            }
        }

        /// <summary>
        /// Helper to raise a PropertyChanged event  />).
        /// </summary>
        private void OnPropertyChanged(string propertyName)
        {
            OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
        }

        #region Deferred work

        // defer work until the current activity completes
        private void DeferAction(Action action)
        {
            if (_deferredActions == null)
            {

View on GitHub (pinned to 81131a70a4)