dotnet/wpf · error · InvalidOperationException

SR.InsertInDeferSelectionActive

Error message

SR.InsertInDeferSelectionActive

What it means

SelectedItemCollection.InsertItem throws when an item is inserted into the SelectedItems collection while the deferred selection update (ChangeSelectedItems/SelectionChange) is active. During a deferred selection pass, only queued Add/Remove of ItemInfo is allowed; inserting at an arbitrary index mid-update is not supported and would corrupt the selection commit.

Solutions

  1. Move the Insert call outside the deferred-selection block / SelectionChanged handler, deferring with Dispatcher.BeginInvoke if needed.
  2. Use SelectedItems.Add instead of Insert at a specific index during selection updates.
  3. End any active UpdateSelectedItems scope before inserting.
  4. Wrap modifications in Selector.UpdateSelectedItems which manages the begin/end scope correctly.

Example fix

// before
listBox.SelectedItems.Insert(0, item); // inside SelectionChanged
// after
Dispatcher.BeginInvoke(new Action(() => listBox.SelectedItems.Insert(0, item)));
Defensive patterns

Strategy: try-catch

Validate before calling

if (!listBox.IsUpdatingSelectedItems && !selectionChangeActive) listBox.SelectedItems.Insert(0, item);

Try / catch

try { listBox.SelectedItems.Insert(0, item); }
catch (InvalidOperationException) { Dispatcher.BeginInvoke(new Action(() => listBox.SelectedItems.Insert(0, item))); }

Prevention

When it happens

Trigger: Calling SelectedItems.Insert(item, index) (with index == Count it routes through Select, otherwise throws) from inside an UpdateSelectedItems block or while a SelectionChange is in progress (e.g. inside a SelectionChanged handler or OnExecutedDelete/MakeFullRowSelection).

Common situations: Manipulating ListBox.SelectedItems inside SelectionChanged event handlers; custom selection logic that calls Insert during a BeginUpdateSelectedItems/EndUpdateSelectedItems block.

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/9c0686a15d203155. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/SelectedItemCollection.cs:86

                }
            }
        }

        /// <summary>
        /// Inserts an item in the selection
        /// </summary>
        protected override void InsertItem(int index, object item)
        {
            if (_updatingSelectedItems)
            {
                // For defered selection we should allow only Add method
                if (index == Count)
                {
                    _selector.SelectionChange.Select(_selector.NewItemInfo(item), true /* assumeInItemsCollection */);
                }
                else
                {
                    throw new InvalidOperationException(SR.InsertInDeferSelectionActive);
                }
            }
            else
            {
                using (ChangeSelectedItems())
                {
                    base.InsertItem(index, item);
                }
            }
        }

        /// <summary>
        /// Sets an item on specified index
        /// </summary>
        protected override void SetItem(int index, object item)
        {
            if (_updatingSelectedItems)
            {

View on GitHub (pinned to 81131a70a4)