dotnet/wpf · error · InvalidOperationException

SR.SetInDeferSelectionActive

Error message

SR.SetInDeferSelectionActive

What it means

SelectedItemCollection.SetItem replaces an item at a given index and is disallowed while _updatingSelectedItems is true (i.e., between BeginUpdateSelectedItems and EndUpdateSelectedItems). Replacing items during a deferred selection commit would bypass the SelectionChange bookkeeping, so it throws InvalidOperationException.

Solutions

  1. Perform the SetItem/assignment after EndUpdateSelectedItems completes.
  2. Queue the change and apply it via Dispatcher once the deferred selection scope ends.
  3. Avoid mutating SelectedItems inside SelectionChanged/cell-selection handlers; use UpdateSelectedItems instead.
  4. Restructure logic to Add/Remove items rather than replace in place.

Example fix

// before
using (deferredScope) { listBox.SelectedItems[0] = newItem; }
// after
using (deferredScope) { /* deferred add/remove only */ }
listBox.SelectedItems[0] = newItem;
Defensive patterns

Strategy: validation

Validate before calling

if (!isInsideUpdateSelectedItemsBlock) listBox.SelectedItems[index] = newItem;

Try / catch

try { listBox.SelectedItems[index] = newItem; }
catch (InvalidOperationException) { Dispatcher.BeginInvoke(new Action(() => listBox.SelectedItems[index] = newItem)); }

Prevention

When it happens

Trigger: Calling SelectedItems[index] = newItem from inside an UpdateSelectedItems lambda/block, or via any code path that runs while the selection change is active (SelectionChanged handler, OnSelectedCellsChanged, MakeFullRowSelection).

Common situations: Custom DataGrid/ListBox selection code that tries to swap selected items inside selection-change callbacks; reentrancy from event handlers triggered during a bulk selection update.

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/0008cdd0736f17e1. Report an issue: GitHub.

Appendix: source

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

                }
            }
            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)
            {
                throw new InvalidOperationException(SR.SetInDeferSelectionActive);
            }
            else
            {
                using (ChangeSelectedItems())
                {
                    base.SetItem(index, item);
                }
            }
        }

        /// <summary>
        /// Movea an item from one position to another
        /// </summary>
        /// <param name="oldIndex">index of the column which is being moved</param>
        /// <param name="newIndex">index of the column to be move to</param>
        protected override void MoveItem(int oldIndex, int newIndex)
        {
            if (oldIndex != newIndex)

View on GitHub (pinned to 81131a70a4)