dotnet/wpf · error · NotSupportedException

SR.UnexpectedCollectionChangeAction (formatted with…

Error message

SR.UnexpectedCollectionChangeAction (formatted with e.Action)

What it means

ValidateCollectionChangedEventArgs's default branch throws NotSupportedException with SR.UnexpectedCollectionChangeAction when e.Action is not one of the five standard NotifyCollectionChangedAction values. Only Add/Remove/Replace/Move/Reset are valid; any other value means a broken or custom event source.

Solutions

  1. Audit the event-raising code so only NotifyCollectionChangedAction.Add/Remove/Replace/Move/Reset are used.
  2. Replace raw enum casts with the static NotifyCollectionChangedEventArgs factory methods.
  3. Catch NotSupportedException around updates and force a view rebuild via Refresh().

Example fix

// before
var args = new NotifyCollectionChangedEventArgs((NotifyCollectionChangedAction)rawValue);
// after
var action = Enum.IsDefined(typeof(NotifyCollectionChangedAction), rawValue)
    ? (NotifyCollectionChangedAction)rawValue
    : NotifyCollectionChangedAction.Reset;
Defensive patterns

Strategy: type-guard

Validate before calling

if (!Enum.IsDefined(typeof(NotifyCollectionChangedAction), args.Action)) { /* reject or fall back to Reset */ }

Type guard

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

Try / catch

try { /* process event */ } catch (NotSupportedException) { view.Refresh(); }

Prevention

When it happens

Trigger: A bound INotifyCollectionChanged source raises CollectionChanged with an undefined enum value (e.g. an out-of-range cast) that reaches the validation switch's default case.

Common situations: Unsafe enum casts in custom collections, code generators or interop layers fabricating NotifyCollectionChangedEventArgs, corrupted event replay/logging frameworks.

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/75312ca41e363a00. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/ListCollectionView.cs:2525

                    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>
        /// Create, filter and sort the local index array.
        /// called from Refresh(), override in derived classes as needed.
        /// </summary>
        /// <param name="list">new ILIst to associate this view with</param>
        /// <returns>new local array to use for this view</returns>
        private void PrepareLocalArray()
        {
            PrepareShaping();

            LiveShapingList lsList = _internalList as LiveShapingList;
            if (lsList != null)
            {
                lsList.LiveShapingDirty -= new EventHandler(OnLiveShapingDirty);

View on GitHub (pinned to 81131a70a4)