dotnet/wpf · error · InvalidOperationException

SR.ChangingCollectionNotSupported

Error message

SR.ChangingCollectionNotSupported

What it means

Selector throws this InvalidOperationException when code modifies the SelectedItems collection while the Selector is in single-selection mode (CanSelectMultiple is false, i.e. SelectionMode is Single). In single-selection mode SelectedItems is maintained by the Selector itself and must not be directly mutated, because any collection change would allow multiple selections, contradicting the mode. The library throws inside OnSelectedItemsCollectionChanged as soon as it observes a direct change to the collection.

Solutions

  1. Switch the control to SelectionMode="Multiple" or "Extended" before mutating SelectedItems
  2. Set the selection via the item's IsSelected property or SelectedItem/SelectedIndex instead of mutating SelectedItems
  3. If the intent is only to change the current selection in single mode, use SelectedItem = value

Example fix

// before
listBox.SelectedItems.Add(item); // SelectionMode is Single -> throws
// after
listBox.SelectionMode = SelectionMode.Multiple;
listBox.SelectedItems.Add(item);
Defensive patterns

Strategy: validation

Validate before calling

if (!listBox.CanSelectMultiple)
    throw new InvalidOperationException("Switch SelectionMode to Multiple/Extended before mutating SelectedItems.");

Try / catch

try
{
    listBox.SelectedItems.Add(item);
}
catch (InvalidOperationException ex)
{
    // fall back: set SelectionMode first or use SelectedItem
}

Prevention

When it happens

Trigger: Calling SelectedItems.Add/Remove/Clear (or any mutation) on a ListBox/Selector whose SelectionMode is Single; assigning a mutated collection in XAML or code; data-binding code that writes into SelectedItems before switching to Multiple/Extended.

Common situations: Developers porting code that worked with a multi-select ListBox to a single-select one; MVVM code that manipulates SelectedItems directly; setting SelectionMode=Single after populating SelectedItems in XAML.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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

Appendix: source

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

                        SelectionChange.Cancel();
                    }
                }
            }

            return succeeded;
        }

        private void OnSelectedItemsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            // Ignore selection changes we're causing.
            if (SelectionChange.IsActive)
            {
                return;
            }

            if (!CanSelectMultiple)
            {
                throw new InvalidOperationException(SR.ChangingCollectionNotSupported);
            }

            SelectionChange.Begin();
            bool succeeded=false;
            try
            {
                switch (e.Action)
                {
                    case NotifyCollectionChangedAction.Add:
                        if (e.NewItems.Count != 1)
                            throw new NotSupportedException(SR.RangeActionsNotSupported);

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

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

View on GitHub (pinned to 81131a70a4)