dotnet/wpf · error · NotSupportedException

SR.UnexpectedCollectionChangeAction

Error message

SR.UnexpectedCollectionChangeAction

What it means

OnSelectedItemsCollectionChanged handles only Add, Remove, Reset, Replace and Move events on SelectedItems; any other NotifyCollectionChangedAction reaches the default case and throws NotSupportedException with UnexpectedCollectionChangeAction. In practice this fires when a custom INotifyCollectionChanged implementation raises a nonstandard or unexpected action against SelectedItems.

Solutions

  1. Only assign collections whose change notifications use the standard NotifyCollectionChangedAction values (Add/Remove/Reset/Replace/Move)
  2. Do not replace the built-in SelectedItems collection; instead drive selection via item IsSelected or SelectedItem
  3. If a custom collection is required, raise Reset for any bulk or unusual change

Example fix

// before
customCollection.RaiseAction((NotifyCollectionChangedAction)99, items); // invalid action
// after
customCollection.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
Defensive patterns

Strategy: validation

Validate before calling

if (!Enum.IsDefined(typeof(NotifyCollectionChangedAction), action))
    throw new NotSupportedException("Unsupported collection change action.");

Try / catch

try
{
    UpdateSelection(e);
}
catch (NotSupportedException ex)
{
    // log and resync selection from SelectedItems
}

Prevention

When it happens

Trigger: Assigning a custom collection to SelectedItems whose RaiseCollectionChanged uses a nonstandard action value; wrappers or AOP proxies that synthesize events with invalid actions; a subclass of ObservableCollection overriding collection-change propagation with custom actions.

Common situations: Advanced MVVM frameworks swapping the internal SelectedItems collection; code generation or interception frameworks mis-raising collection-change events; porting code that relied on nonstandard actions in other collection implementations.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Primitives/Selector.cs:912

                        for (int i = 0; i < userSelectedItems.Count; i++)
                        {
                            SelectionChange.Select(NewUnresolvedItemInfo(userSelectedItems[i]), false /* assumeInItemsCollection */);
                        }
                        break;

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

                        SelectionChange.Unselect(NewUnresolvedItemInfo(e.OldItems[0]));
                        SelectionChange.Select(NewUnresolvedItemInfo(e.NewItems[0]), false /* assumeInItemsCollection */);
                        break;

                    case NotifyCollectionChangedAction.Move:
                        break;  // order within SelectedItems doesn't matter

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

                SelectionChange.End();
                succeeded = true;
            }
            finally
            {
                if (!succeeded)
                {
                    SelectionChange.Cancel();
                }
            }
        }

        #endregion


        //-------------------------------------------------------------------

View on GitHub (pinned to 81131a70a4)